在API 11之后,Android为了支持ViewGroup类控件,在添加和移除其中控件时自动添加动画,为我们提供了一个非常简单的属性:android:animateLayoutChanges=[true/false],所有派生自ViewGroup的控件都具有此属性,只要在XML中添加上这个属性,就能实现添加/删除其中控件时,带有默认动画了。


效果如下所示:


菜单资源文件mymenu.xml配置如下:

<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto">    <item        android:id="@+id/action_add"        android:title="add"        app:showAsAction="always"        android:icon="@android:drawable/ic_input_add"/></menu>

主布局文件activity_main.xml文件配置如下:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    android:id="@+id/rootView"    android:animateLayoutChanges="true"    tools:context="com.example.icarus.layoutchangeanim.MainActivity"></LinearLayout>

主程序MainActivity:

package com.example.icarus.layoutchangeanim;/** * 项目:添加或删除视图是添加动画效果 * 使用功能: * android:animateLayoutChanges="true" * 实现方式: * 在主布局上添加一个菜单项,设置图标,功能为添加一个按钮到主布局当中 * 点击按钮则实现删除当前按钮 */import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.Button;import android.widget.LinearLayout;public class MainActivity extends AppCompatActivity {    private LinearLayout rootView;    //创建 View.OnClickListener变量,并初始化实现其功能    private View.OnClickListener btn_onClickListener=new View.OnClickListener() {        @Override        public void onClick(View v) {            rootView.removeView(v);        }    };    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        if(findViewById(R.id.rootView)!=null){            rootView= (LinearLayout) findViewById(R.id.rootView);        }    }    /**     * 自定义函数,实现添加按钮到视图上的功能     */    public void addButton(){        Button btn =new Button(this);        btn.setText("Remove me");        rootView.addView(btn);        btn.setOnClickListener(btn_onClickListener);    }    /**     * 创建菜单到视图中     * @param menu     * @return     */    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.mymenu, menu);        return true;    }    /**     * 菜单项功能实现     * @param item     * @return     */    @Override    public boolean onOptionsItemSelected(MenuItem item) {        // Handle action bar item clicks here. The action bar will        // automatically handle clicks on the Home/Up button, so long        // as you specify a parent activity in AndroidManifest.xml.        switch (item.getItemId()) {            case R.id.action_add:                addButton();                break;            default:                break;        }        return super.onOptionsItemSelected(item);    }}

更多相关文章

  1. Android之Adapter(适配器)
  2. Android使用LayoutInflater动态加载布局和操作控件
  3. Android(安卓)Studio3.4 中无法找到Android(安卓)Device Monitor
  4. Android文字跑马灯控件(文本自动滚动控件)
  5. Android开发之日历控件实现
  6. Android安全框架:Verfied boot -- Secure Boot
  7. android利用数据库实现搜索联想功能
  8. Android(安卓)事件拦截机制一种粗鄙的解释
  9. CropImageView android上的一个图片裁剪控件

随机推荐

  1. android使用Navigation实现Fragment之间
  2. android利用WebView与JavaScript交互的方
  3. 程序包android.support.annotation不存在
  4. Android(安卓)8.0整体编译成功后使用mmm
  5. 通过Html网页调用本地安卓(android)app
  6. Android监控程序本身被卸载方法汇总
  7. Android(安卓)6.0 RxJava2 + RxPermissio
  8. Android扫码 有仿微信版
  9. android Gallery 实现短距离滚动 取消惯
  10. 安装Android