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();
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。