Android中的Selector的用法

Android中的Selector主要是用来改变ListView和Button控件的默认背景。其使用方法可以按一下步骤来设计:


(以在mylist_view.xml为例)


1.创建mylist_view.xml文件

首先在res目录下新建drawable文件夹,再在新建的drawable文件夹中新建mylist_view.xml,其目录结构为:res/drawable/mylist_view.xml。


2.根据具体需求编辑mylist_view.xml文件

新建mylist_view.xml文件后,在没有添加任何属性时其内部代码结构为:

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8" ?>     
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">   
  3.    
  4. </selector>  

下面就可以根据项目需求,在其内部定义为自己想要的样式了,主要属性如下:

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8" ?>     
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">   
  3. <!-- 默认时的背景图片-->    
  4.   <item android:drawable="@drawable/pic1" />      
  5. <!-- 没有焦点时的背景图片 -->    
  6.   <item android:state_window_focused="false"     
  7.         android:drawable="@drawable/pic1" />     
  8. <!-- 非触摸模式下获得焦点并单击时的背景图片 -->    
  9.   <item android:state_focused="true" android:state_pressed="true"   android:drawable"@drawable/pic2" />   
  10. <!-- 触摸模式下单击时的背景图片-->    
  11. <item android:state_focused="false" android:state_pressed="true"   android:drawable="@drawable/pic3" />    
  12. <!--选中时的图片背景-->    
  13.   <item android:state_selected="true"   android:drawable="@drawable/pic4" />     
  14. <!--获得焦点时的图片背景-->    
  15.   <item android:state_focused="true"   android:drawable="@drawable/pic5" />     
  16. </selector>  

3.引用 mylist_view.xml文件


三种方法可以来引用刚才创建的文件:

(1)在ListView中添加如下属性代码

[html]  view plain copy
  1. android:listSelector="@drawable/mylist_view"  

(2)在 ListView的item界面中添加如下属性代码

[html]  view plain copy
  1. android:background="@drawable/mylist_view"  

(3)利用JAVA代码直接编写

[java]  view plain copy
  1. Drawable drawable = getResources().getDrawable(R.drawable.mylist_view);   
  2. listView.setSelector(drawable);  

为了防止列表拉黑的情况发生,需要在 ListView中添加以下的属性代码

[html]  view plain copy
  1. android:cacheColorHint="@android:color/transparent"  

属性介绍:

android:state_selected选中

android:state_focused获得焦点

android:state_pressed压着的

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


看一个checkBox利用selector实现变化的例子定制个性化checkBox


注意,这三种方法不可以同时使用~~~

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
AndroidSelector是一个用于定义不同状态下View的样式和背景的XML文件。它可以根据View的不同状态(如按下、选、禁用等)来设置不同的样式和背景。下面是Selector用法详解及实例。 1. 创建selector xml文件 在res/drawable目录下创建一个XML文件,文件名以selector_开头,如selector_button.xml,然后在文件定义不同状态下View的样式和背景。例如: ```xml <selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 按下状态 --> <item android:drawable="@drawable/button_pressed" android:state_pressed="true" /> <!-- 选状态 --> <item android:drawable="@drawable/button_selected" android:state_selected="true" /> <!-- 默认状态 --> <item android:drawable="@drawable/button_normal" /> </selector> ``` 2. 在View应用selector 在布局文件使用android:background属性来应用selector,如: ```xml <Button android:id="@+id/btn_login" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="登录" android:background="@drawable/selector_button" /> ``` 这样,当Button的状态改变时,它的背景就会自动变化。 除了android:background属性外,还可以在TextView、EditText等View使用android:textColor属性来应用selector,如: ```xml <TextView android:id="@+id/tv_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" android:textColor="@drawable/selector_text_color" /> ``` 其selector_text_color.xml文件的内容如下: ```xml <selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 按下状态 --> <item android:color="@color/colorAccent" android:state_pressed="true" /> <!-- 选状态 --> <item android:color="@color/colorPrimary" android:state_selected="true" /> <!-- 默认状态 --> <item android:color="@android:color/black" /> </selector> ``` 这样,当TextView的状态改变时,它的文字颜色就会自动变化。 以上就是SelectorAndroid用法详解及实例。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值