标签:selector不起作用
1、由于TextView,RelativeLayout(LinearLayout) 默认不具备可点击性的,要让background的selector 起作用有两种方法:
一、在代码中给其设置点击事件OnClickListener
二、在xml中设置clickable = true
2、selector写的不对,不点击时的默认效果应该放在最后面。如下面这样,会不起作用:
<selectorxmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/assemble_item_bg"/>
<itemandroid:drawable="@drawable/assemble_item_bg_pressed"android:state_enabled="true"android:state_focused="true"android:state_pressed="false"/>
<itemandroid:drawable="@drawable/assemble_item_bg_pressed"android:state_enabled="true"android:state_pressed="true"/>
<itemandroid:drawable="@drawable/assemble_item_bg_pressed"android:state_focused="true"android:state_pressed="true"/>
</selector>
应该这样才有点击效果:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/assemble_item_bg_pressed" android:state_enabled="true" android:state_focused="true" android:state_pressed="false"/>
<item android:drawable="@drawable/assemble_item_bg_pressed" android:state_enabled="true" android:state_pressed="true"/>
<item android:drawable="@drawable/assemble_item_bg_pressed" android:state_focused="true" android:state_pressed="true"/>
<item android:drawable="@drawable/assemble_item_bg"/>
</selector>
注意:用颜色值而不是用图片作为selector时,要这样用:
<selectorxmlns:android="http://schemas.android.com/apk/res/android">
<itemandroid:drawable="@color/auto_complete_textview_normal_pressed"android:state_pressed="true"/>
<itemandroid:drawable="@color/auto_complete_textview_normal"/>
</selector>
版权声明:本文为博主原创文章,未经博主允许不得转载。
给TextView,RelativeLayout 设置selector不起作用的原因总结
标签:selector不起作用
原文地址:http://blog.csdn.net/nnmmbb/article/details/47777831