android 选择器selector的用法说明

时间:2023-02-14 03:51:50 作者:嗷呜大恐龙 综合材料 收藏本文 下载本文

【导语】“嗷呜大恐龙”通过精心收集,向本站投稿了2篇android 选择器selector的用法说明,以下是小编为大家准备的android 选择器selector的用法说明,供大家参考借鉴,希望可以帮助到有需要的朋友。

篇1:android 选择器selector的用法说明

一.创建xml文件,位置:drawable/xxx.xml,同目录下记得要放相关图片

android:drawable=“@drawable/pic1” />

android:state_pressed=“true”

android:drawable= “@drawable/pic2” />

android:state_pressed=“true”

android:drawable=“@drawable/pic3” />

android:drawable=“@drawable/pic4” />

android:drawable=“@drawable/pic5” />

二.使用xml文件:

1.方法一:在listview中配置android:listSelector=“@drawable/xxx

或者在listview的item中添加属性android:background=”@drawable/xxx“

2.方法二:Drawable drawable = getResources.getDrawable(R.drawable.xxx);

ListView.setSelector(drawable);但是这样会出现列表有时候为黑的情况,需要加上:android:cacheColorHint=”@android:color/transparent“使其透明,

相关属性:

android:state_selected是选中

android:state_focused是获得焦点

android:state_pressed是点击

android:state_enabled是设置是否响应事件,指所有事件

根据这些状态同样可以设置button的selector效果。也可以设置selector改变button中的文字状态。

以下是配置button中的文字效果:

drawable/button_font.xml

Button还可以实现更复杂的效果,例如渐变

drawable/button_color.xml

/

//定义当button 处于pressed 状态时的形态,

android:startColor=”#8600ff“ />

android:width=”2dp“ android:color=”#000000“ />

android:bottom=”10dp“ android:right=”10dp“/>

//定义当button获得 focus时的形态

color=”#ffffff“/>

android:bottom=”10dp“ android:right=”10dp“/>

最后,需要在包含 button的xml文件里添加两项。例如main.xml 文件,需要在里加两项android:focusable=”true“

android:background=”@drawable/button_color“

篇2:Android:自定义控件样式(Selector)

概要实现

首先写这个Selector XML文件,叫做button_selector,放到了drawable文件夹下,大概内容如下所示

复制代码

1

2

3

4  

5  

6    

7       

8      

9      

10      

11

12

13

14  

15  

16    

17      

18      

19      

20      

21

22

23

24

复制代码

然后为该按钮设置background属性:@drawable/button_selector,如下所示

复制代码

android:id=”@+id/btnSelector“

android:layout_width=”wrap_content“

android:layout_height=”wrap_content“

android:background=”@drawable/button_selector“

android:text=”Selector演示“

/>

复制代码

这样自定义样式就成功的应用到了这个按钮上了,

Android:自定义控件样式(Selector)

Selector

先来看下官方描述:

You can describe the state list in an XML file. Each graphic is represented by an element inside a single element. Each uses various attributes to describe the state in which it should be used as the graphic for the drawable.

意思是说:你可以用一个XML文件来描述状态列表。在唯一的selector节点下,用item来描述每一种状态。每一个item通过不同的属性来标识用于哪种属性。

下面就看下item的具体属性

android:drawable:引用一个drawable资源

android:state_pressed:Boolean值,如果设置为true则代表用于对象在被按下的时候

android:state_focused:Boolean值,如果设置为true则代表用于对象在获得焦点的时候

android:state_hovered:Boolean值,如果设置为true则代表用于对象在hover状态的时候

android:state_selected:Boolean值,如果设置为true则代表用于对象在选中的时候

android:state_checkable:Boolean值,如果设置为true则代表用于对象允许选中的时候

android:state_checked:Boolean值,如果设置为true则代表用于对象被选中的时候

android:state_enabled:Boolean值,如果设置为true则代表用于对象可用的时候(响应触摸或点击事件)

android:state_activated:Boolean值,如果设置为true则代表用于对象被激活的时候

android:state_window_focused:Boolean值,如果设置为true则代表用于窗体获得焦点的时候

通过以上属性,就可以灵活的定制出期望的结果了,这次例子只是展示了android:state_pressed这一种状态的效果。Selector代码为上文提到的button_selector。效果如下所示

简单分析

再来看下press的那个item内容。

复制代码

复制代码

上图所示的按下效果是通过shape标签来完成的。这个标签用来指定背景的样式,由于这次重点介绍Selector的用法,Shape的用法就不过多解释了,只是把代码中出现的标签做下简单说明。

stroke:用来设定背景边框的样式,可以去定义它的宽度(width),颜色(color),是否为虚线展示等等

android定时器

android位置传感器

android实习总结报告模板

android属性动画Property

Android Studio 安装使用

Android Dialog的九种常用形式

Android应用界面设计交互设计

android构造方法的作用

android简历怎么写项目经验

android实训实习报告总结

android 选择器selector的用法说明(推荐2篇)

欢迎下载DOC格式的android 选择器selector的用法说明,但愿能给您带来参考作用!
推荐度: 推荐 推荐 推荐 推荐 推荐
点击下载文档 文档为doc格式
点击下载本文文档