android space控件用法,Android的ripple,Space,merge,include,ViewStub标签的使用

本文介绍了Android中Ripple效果的实现,包括无边界和有边界的使用方法。此外,还详细讲解了Space标签在布局间隔中的应用,include标签如何实现布局复用,merge标签的优化布局功能及其限制,以及ViewStub标签的按需加载特性,以减少内存消耗。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Android的ripple,Space,merge,include,ViewStub标签的使用

1.Android5.0 ripple标签

在Android 5.0后加入ripple标签,使用这个Drawable做控件的背景,在点击的时候就可以达到波浪效果。

ripple标签对应是一个rippleDrawable,当使用它作为背景的时候,在控件按下去的时候,就是显示水波效果。

在res目录下的drawable目录下创建ripple标签

bVcOSXc

ripple主要有两种形式

1.没有边界的ripple

这种没有边界的ripple只需要设置 ripple color属性就行了,不用给他添加item

android:color="@color/bg_press">

2.有边界的ripple

这种有边界的需要给ripple添加一个item,item可以是图片,纯颜色,shape,selector.

android:color="@color/bg_press">

//这是一个shape

注意:如果想控件在不被点击的时候背景不显示,这个时候需要给item设置id=@android:id/mask 否则控件的背景就是 item 的资源了

在 5.0 之前 forground = ?attr/selectableItemBackground 可以实现波纹效果

2.Android的Space标签

Android Space标签是Android4.0添加,是一个轻量级的View,一般用于分割组件,布局或者在组件布局之间产生间隔。

android:layout_width="0dp"

android:layout_height="0dp"

android:layout_weight="1" />

3.Android的include标签

创建一个include_test布局文件<?xml version="1.0" encoding="utf-8"?>

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

android:layout_height="wrap_content"

android:text="我是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.只可以作为xml layout的根节点,创建一个merge_test布局

android:layout_height="wrap_content"

android:text="我是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设置到代码;不可以设置固定值的宽高

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布局

android:layout_width="match_parent"

android:layout_height="match_parent">

android:layout_height="wrap_content"

android:text="我是ViewStub标签"/>

使用ViewStub标签加载stub_test布局

静态界面没有显示只有调用才会显示

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();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值