Android中的状态选择器
16lz
2021-12-04
Android中的状态选择器
概述 在drawable/xxx.xml中配置,通过配置selector,可以使系统运行时根据控件对象的状态使用相应的图片、文字等。
selector中的常用属性
使用方法
还可以实现更复杂的效果,例如渐变等等。 drawable/button_color.xml
shape标签: 在shape里面有个shape属性,这个属性可以设定,也可以不设定,不设定的时候默认是矩形。设定有四个值可以设定:
angle表示颜色渐变的起始位置,0表示从左向右然后逆时针方向,90表示从上到下,以此类推,angle必须为45点整数倍 startColor endColor centerColor,颜色 渐变 过程的颜色值。 type,颜色渐变类型,有三个值 1、linear,线性渐变,这个是默认值 2、radial,放射性渐变,这个要配合android:gradientRadius属性使用,android:gradientRadius表示放射渐变的半径大小。 3、sweep,扫描石渐变,就像雷达扫描的那个效果。 centerX,centerY,表示渐变中心的X和Y点的坐标的相对位置。
概述 在drawable/xxx.xml中配置,通过配置selector,可以使系统运行时根据控件对象的状态使用相应的图片、文字等。
selector中的常用属性
android:state_selected 控件选中状态,可以为true或false android:state_focused 控件获得焦点状态,可以为true或false android:state_pressed 控件点击状态,可以为true或false android:state_enabled 控件使能状态,可以为true或false android:state_checkable 控件可勾选状态,可以为true或false android:state_checked 控件勾选状态,可以为true或false android:window_focused 应用程序窗口焦点状态,可以为true或false android:color 定义特定状态的颜色注意:在状态描述中,第一个匹配当前状态的item会被使用。因此,如果第一个item没有任何状态特性的话,那么它将每次都被使用,所以默认的值必须总是在最后。
使用方法
第一种是在listview中配置android:listSelector=”@drawable/list_item_bg” 第二种是在listview的item中添加属性android:background=”@drawable/list_item_bg” 第三种是java代码中使用: Drawable drawable = getResources().getDrawable(R.drawable.list_item_bg); listview.setSelector(drawable); 注:列表有时候为黑的情况,需要加上下面的代码使其透明: android:cacheColorHint="@android:color/transparent"demo分析:
还可以实现更复杂的效果,例如渐变等等。 drawable/button_color.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<gradient android:startColor="#8600ff" />
<stroke android:width="2dp"
android:color="#000000" />
<corners android:radius="5dp" />
<padding android:left="10dp"
android:top="10dp"
android:bottom="10dp"
android:right="10dp"/>
shape>
item>
<item android:state_focused="true">
<gradient android:startColor="#eac100"/>
<stroke android:width="2dp"
android:color="#333333"
color="#ffffff"/>
<corners android:radius="8dp" />
<padding android:left="10dp"
android:top="10dp"
android:bottom="10dp"
android:right="10dp"/>
shape>
item>
selector>
shape标签: 在shape里面有个shape属性,这个属性可以设定,也可以不设定,不设定的时候默认是矩形。设定有四个值可以设定:
1、rectangle 矩形 2、oval 椭圆形 当宽高设定为相同的时候,就是圆 3、line 线性形状 4、ring 环形 可用作数据刷新时转圈的提示 当设定为ring环形的时候,还需要设定一下几个属性
gradient标签:android:innerRadiusRatio="3" 浮点型数据,以环的宽度比率来表示内环的半径android:thicknessRatio="8" 浮点型数据,以环的宽度比率来表示环的厚度android:useLevel="false" 如果当做是LevlListDrawable使用时为true,其他为false
angle表示颜色渐变的起始位置,0表示从左向右然后逆时针方向,90表示从上到下,以此类推,angle必须为45点整数倍 startColor endColor centerColor,颜色 渐变 过程的颜色值。 type,颜色渐变类型,有三个值 1、linear,线性渐变,这个是默认值 2、radial,放射性渐变,这个要配合android:gradientRadius属性使用,android:gradientRadius表示放射渐变的半径大小。 3、sweep,扫描石渐变,就像雷达扫描的那个效果。 centerX,centerY,表示渐变中心的X和Y点的坐标的相对位置。
更多相关文章
- shape 的创建和使用
- Android(安卓)Launcher 分析
- Android(安卓)控件ProgressBar进度条
- Android中自定义控件
- Android中状态栏的隐藏
- android: layout_alignParentRight android_paddingRight
- 系出名门Android(5) - 控件(View)之TextView, Button, ImageButt
- 相对布局的属性
- Androidc学习笔记二之四大布局及碎片理解用法