关于android:Android的rippleSpacemergeincludeViewStub标签的使用

9次阅读

共计 3124 个字符,预计需要花费 8 分钟才能阅读完成。

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();
正文完
 0