android,自定义state_pressed不起作用

时间:2015-09-03 12:31:04

标签: android android-custom-view android-custom-drawable

我的自定义选择器如下所示无法正常工作,我根本看不到按下状态。有提示吗?感谢

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"

    >
    <!-- this state button has special design:
        + enabled state has white text on green background
        + disabled state has grey text on greyer background
    -->


    <item android:state_enabled="true"
        android:drawable="@drawable/cb_shape_button_primary"
        android:color="@color/white"

        />
    <item android:state_pressed="true"
        android:drawable="@drawable/cb_shape_button_pressed"
        android:color="@color/white"

        />

    <item android:state_focused="true"
        android:drawable="@drawable/cb_shape_button_pressed"
        android:color="@color/white"

        />
    <item android:state_enabled="false"
        android:drawable="@drawable/cb_shape_button_flat"
        android:color="@color/grey_subtext"
        />

</selector>

4 个答案:

答案 0 :(得分:5)

尝试将state_pressed移动到顶部,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"

>
<!-- this state button has special design:
    + enabled state has white text on green background
    + disabled state has grey text on greyer background
-->

<item android:state_pressed="true"
    android:drawable="@drawable/cb_shape_button_pressed"
    android:color="@color/white"

    />

<item android:state_enabled="true"
    android:drawable="@drawable/cb_shape_button_primary"
    android:color="@color/white"

    />

<item android:state_focused="true"
    android:drawable="@drawable/cb_shape_button_pressed"
    android:color="@color/white"

    />
<item android:state_enabled="false"
    android:drawable="@drawable/cb_shape_button_flat"
    android:color="@color/grey_subtext"
    />

答案 1 :(得分:1)

选择器XML标记始终选择第一个匹配项。这意味着如果启用了视图,它将始终显示@drawable/cb_shape_button_primary。选择器的一般经验法则是您的“默认”项应该是列表中的最后一项。

尝试最后移动默认状态,看看是否能解决您的问题。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"

    >
    <!-- this state button has special design:
        + enabled state has white text on green background
        + disabled state has grey text on greyer background
    -->

    <item android:state_pressed="true"
        android:drawable="@drawable/cb_shape_button_pressed"
        android:color="@color/white"

        />

    <item android:state_focused="true"
        android:drawable="@drawable/cb_shape_button_pressed"
        android:color="@color/white"

        />
    <item android:state_enabled="false"
        android:drawable="@drawable/cb_shape_button_flat"
        android:color="@color/grey_subtext"
        />

   <item android:state_enabled="true"
        android:drawable="@drawable/cb_shape_button_primary"
        android:color="@color/white"

        />

</selector>

答案 2 :(得分:0)

试试这个,

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/select_item" android:state_checked="true" android:state_focused="true" />
<item android:drawable="@drawable/unselect_item" android:state_checked="false" android:state_focused="true" />
<item android:drawable="@drawable/unselect_item" android:state_checked="false" />
<item android:drawable="@drawable/select_item" android:state_checked="true" />

答案 3 :(得分:0)

对于简单选择和取消选择使用此

<selector><item android:state_checked="false" android:drawable="@drawable/radiobtnunselected"/><item android:state_checked="true" android:drawable="@drawable/radiobtnselected"/></selector>

设置你的图像在android:drawable

True:已选中

错误:未选中