Android的ripple,Space,merge,include,ViewStub标签的应用
1.Android5.0 ripple标签
在Android 5.0后退出ripple标签,应用这个Drawable做控件的背景,在点击的时候就能够达到波浪成果。
ripple标签对应是一个rippleDrawable,当应用它作为背景的时候,在控件按上来的时候,就是显示水波成果。
在res目录下的drawable目录下创立ripple标签
ripple次要有两种模式
1.没有边界的ripple
这种没有边界的ripple只须要设置 ripple color属性就行了,不必给他增加item
<ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="@color/bg_press"> </ripple>
2.有边界的ripple
这种有边界的须要给ripple增加一个item,item能够是图片,纯色彩,shape,selector.
<ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="@color/bg_press"> <!--Android 5.0退出的,点击时候能够达到波浪成果--> <item android:drawable="@drawable/bg_my_right" />//这是一个shape </ripple>
留神:如果想控件在不被点击的时候背景不显示,这个时候须要给item设置id=@android:id/mask 否则控件的背景就是 item 的资源了
在 5.0 之前 forground = ?attr/selectableItemBackground 能够实现波纹成果
2.Android的Space标签
Android Space标签是Android4.0增加,是一个轻量级的View,个别用于宰割组件,布局或者在组件布局之间产生距离。
<Space android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="1" />
3.Android的include标签
创立一个include_test布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="我是include标签"/> </LinearLayout>
应用include标签引入布局文件达到共享雷同布局文件
<include android:id="@+id/my_include" layout="@layout/include_test"//通过layout来引入布局文件 android:layout_width="wrap_content" android:layout_height="wrap_content" />
4.Android的merge标签
1.merge标签可用于缩小视图层级来优化布局能够配合include应用
2.<merge/>只能够作为xml layout的根节点,创立一个merge_test布局
<merge xmlns:android="http://schemas.android.com/apk/res/android"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="我是merge标签"/> </merge>
3..当须要inflate的xml layout自身是由merge作为根节点的话,须要将被导入的xml layout置于viewGroup中,同时须要设置attachToRoot为True。
// 必须attachToRoot为true View view = LayoutInflater.from(context).inflate(R.layout.merge_test, this, true);
3.毛病:跟布局merge不能设置padding(设置的padding不起作用),能够把设置在跟布局的padding设置到代码;不能够设置固定值的宽高
<merge xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="280dp" android:paddingLeft="12dp" android:paddingRight="12dp" android:paddingTop="10dp" android:paddingBottom="10dp"> //layout_height="280dp"和:paddingLeft="12dp"这些都不会起作用,须要在代码中设置;
5.Android的ViewStub标签
ViewStub标签按需加载,顾名思议须要的时候再去加载,不须要的时候能够不必加载,节约内存应用
通常状况下会应用setVisibility办法来管制视图的显示和暗藏,然而这种状况视图曾经加载了。
创立一个stub_test布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="我是ViewStub标签"/> </LinearLayout>
应用ViewStub标签加载stub_test布局
动态界面没有显示只有调用才会显示
<ViewStub android:id="@+id/viewstub" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout="@layout/stub_test"/>
留神:ViewStub的inflate()办法只能被调用一次,一旦调用后,ViewStub将从视图中移除,被对应的layout布局取代,同时会保留ViewStub上设置的属性成果
ViewStub viewstub = findViewById(R.id.viewstub); viewstub.inflate();