Android selector的属性和使用详解

来源:互联网 发布:一建刷题软件 编辑:程序博客网 时间:2024/05/23 00:08

1.selector中全部属性


2.常用的属性分析

android:state_accessibility_focused是否能够获取焦点
android:state_selected是否选中
android:state_focused是否获得焦点
android:state_pressed是否点击
android:state_enabled设置是否响应事件,指所有事件
android:state_checkable是否可能选中
android:state_checked否是选中
android:state_active是否活动
android:state_activated
android:state_window_focused

3.部分使用代码片(其他的只能看你自己的需求自己搭配)

<?xml version="1.0" encoding="utf-8" ?>     <selector xmlns:android="http://schemas.android.com/apk/res/android">     <!-- 默认时的背景图片-->      <item android:drawable="@drawable/pic1" />      <!-- 没有焦点时的背景图片 -->      <item android:state_window_focused="false" android:drawable="@drawable/pic1" />     <!-- 非触摸模式下获得焦点并单击时的背景图片 -->      <item android:state_focused="true" android:state_pressed="true"   android:drawable= "@drawable/pic2" />   <!-- 触摸模式下单击时的背景图片-->    <item android:state_focused="false" android:state_pressed="true"   android:drawable="@drawable/pic3" />    <!--选中时的图片背景-->      <item android:state_selected="true"   android:drawable="@drawable/pic4" />     <!--获得焦点时的图片背景-->      <item android:state_focused="true"   android:drawable="@drawable/pic5" /></selector>  
这里是在checkbox中和radiobutton中使用,checkbox可以当个的切换背景,radiobutton可以多个的在Radiogroup中切换背景
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android" ><!--选中背景 -->    <item android:state_checked="true" android:drawable="@drawable/icon_tabfriends_selected"></item><!--没有选中背景 -->    <item android:state_checked="false" android:drawable="@drawable/icon_tabfriends_normal"></item><!--默认背景,这里可要可不要 --><item android:drawable="@drawable/icon_tabfriends_normal"></item></selector>

4.使用的三种方式

(1)在listview中添加属性

android:listSelector="@drawable/xxx
(2)listview的item中添加属性

android:background="@drawable/xxx"
(3)在java代码中

    Drawable drawable = getResources().getDrawable(R.drawable.xxx);       listView.setSelector(drawable);  
为了防止listview滑动变黑,需要在listview中添加

android:cacheColorHint="@android:color/transparent"

可能有些时候你还要用代码去控制一下,单纯的布局还是不行,比如ImageButton你就没有办法去在布局中实现RadioButton的那种效果。


0 0
原创粉丝点击