您的位置:首页 > 移动开发 > Android开发

Android drawable selector 设置无效

2015-08-14 00:37 351 查看
今天写代码时,需要设置一个TextView 的点击效果,press 状态和normal 状态两个icon,同时,点击区域背景在press状态下也不同。实现时将TextView 放在RelativeLayout 中 ,RelativeLayout 作点击区域,设置点击的背景效果,TextView 在点击时显示不同的icon图片。

布局文件如下

[code]<RelativeLayout
    android:id="@+id/dele_layout"
    android:layout_width="math_parent"
    android:layout_height="48dp"
    android:background="@drawable/setting_delete_selector"
    >
    <TextView
        android:id="@+id/delete_move_notice"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="@drawable/webview_move_setting_close"
        />

</RelativeLayout>


点击区域背景setting_delete_selector.xml 文件如下

[code]<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/setting_move_webview_bg"></item>
<item android:drawable="@color/setting_move_action_setting__close_press_bg" android:state_pressed="true"></item>


TextView的selector 文件

[code]<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/pop_ico_delete_normal"></item>
<item android:drawable="@drawable/pop_ico_delete_press" android:state_pressed="true"></item>


其中 color.xml 中定义了 selector.xml 中使用的颜色值。这里就不再贴出。

这样的代码,打死都没有效果,没有效果,没有效果。重要事情说三遍!!!

费劲千辛万苦,查找所有代码,重写demo测试,最后查找出来原因竟然是

press状态要放在selector 的第一行

将两个文件修改如下:

[code]<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/pop_ico_delete_press" android:state_pressed="true"></item>
<item android:drawable="@drawable/pop_ico_delete_normal"></item>
</selector>


这样,效果就有了~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: