总结StateListDrawable(selector)用法

来源:互联网 发布:长兴岛造船基地 知乎 编辑:程序博客网 时间:2024/06/05 03:26

    今天测试了一把selector,总结一下。它内容不多,也不是很难,但是有比较高的可能性卡住我等新手一天半天的。可能就是因为它的内容少才让我们摸不清吧。官方介绍:http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList

    先介绍我的总结,再贴上测试的代码:

1.系统是从上往下匹配的,如果匹配到一个item那么它就将采用这个item,而不是采用的最佳匹配的规则,所以设置缺省的状态,一定要写在最后,如果把缺省的写在前面,那么这样后面所有的item就都不会起作用了。

2.item节点里有很多属性,不同种类的组件支持的item节点的属性并不一定相同,那么该selector资源所对应的组件并不一定支持该属性,所以就会使某些不被组件支持的属性无效。记得之前第一次用selector的时候,想设置个按下(android:state_pressed)和不按下状态不同的背景,设置在ImageView上就是无效的,那是查资料,查到的结果是只能listview和button才可以使用selector。现在来看是因为listview和button支持android:state_pressed这个属性。比如现在我想把一个LinearLayout设置按下/松开不同的背景,那么就把背景用selector,并且设置上属性android:clickable="true"就可以了。

3.关于android:state_focused这个属性,这个属性有点奇葩。首先一个EditText设置获得/失去焦点不同状态---->正常有效。那么拿一个button来说,首先这个button是android:focusable="true"的,默认就是true。然后点击它,它是获得不了焦点的。然后还有一个属性android:focusableInTouchMode="true"(该属性默认是false),设置这个属性为true,点击该button,它就能获得焦点了。在这里我总结,组件获取焦点有两种方式:一种是touch模式,一种是非touch模式。非touch模式是怎么设置焦点呢,view没有setFocusable(boolean)这个方法,但是有这么一个节点,在布局文件里->view节点里面-><requestFocus />。也就是说想让组件获得焦点,需要


android:focusable="true"并且设置在对应模式为true的情况下,用指定的方法是组件获得焦点。如果是touch模


式,可以用touch改变焦点所在的组件,但是如果不是touch模式,据我所知只能在xml里设置下焦点,怎么在程序运


行期间改变组件的焦点呢?如果哪位大神指导,望不吝赐教。PS:可以看到EditText的

EditText.isFocusableInTouchMode()结果是true。



代码:


xml_selector.xml:

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:drawable="@drawable/i1" android:state_checked="true" android:state_pressed="false" android:state_focused="true"/>    <item android:drawable="@drawable/i2" android:state_checked="true" android:state_pressed="true" android:state_focused="true"/>    <item android:drawable="@drawable/i3" android:state_checked="false" android:state_pressed="true" android:state_focused="true"/>    <item android:drawable="@drawable/i4" android:state_checked="false" android:state_pressed="false" android:state_focused="true"/>        <item android:drawable="@drawable/i5" android:state_checked="true" android:state_pressed="false" android:state_focused="false"/>    <item android:drawable="@drawable/i6" android:state_checked="true" android:state_pressed="true" android:state_focused="false"/>    <item android:drawable="@drawable/i7" android:state_checked="false" android:state_pressed="true" android:state_focused="false"/>    <item android:drawable="@drawable/i8" android:state_checked="false" android:state_pressed="false" android:state_focused="false"/></selector>


activity_main2.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <RadioGroup        android:id="@+id/rg"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:orientation="horizontal" >        <RadioButton            android:id="@+id/rb1"            android:layout_width="150dp"            android:layout_height="150dp"            android:background="@drawable/xml_selector"            android:button="@null"            android:text="rb1"             />        <RadioButton            android:id="@+id/rb2"            android:layout_width="150dp"            android:layout_height="150dp"            android:background="@drawable/xml_selector"            android:layout_marginLeft="20dp"            android:button="@null"            android:text="rb2" />    </RadioGroup>    <Button        android:id="@+id/btn"        android:layout_width="200dp"        android:layout_height="100dp"        android:layout_marginTop="30dp"        android:focusable="true"        android:focusableInTouchMode="true"        android:background="@drawable/xml_selector" >        <requestFocus />    </Button>    <EditText        android:id="@+id/et1"        android:layout_width="200dp"        android:layout_height="100dp"        android:layout_marginTop="30dp"        android:background="@drawable/xml_selector" >    </EditText>    <EditText        android:id="@+id/et2"        android:layout_width="200dp"        android:layout_height="100dp"        android:layout_marginTop="30dp"        android:background="@drawable/xml_selector" >    </EditText></LinearLayout>




0 0
原创粉丝点击