TextView selector不起作用

http://blog.csdn.net/nnmmbb/article/details/47777831

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>

展开阅读全文

没有更多推荐了,返回首页