颜色状态列表不适用于形状笔划 - Android

时间:2013-12-31 10:58:38

标签: android xml

我试图让我的形状的笔触根据状态(编辑文本框)改变颜色,但它总是显示为默认颜色。我已经尝试进入/可以从/颜色无效,仍然无法正常工作。出于某种原因,似乎忽略了state_pressed和state_focused ......任何想法?

状态选择器当前位于/ color:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:color="@color/LIGHTBLUE"/>   <!-- pressed -->
    <item android:state_focused="true" android:color="@color/ORANGE"/>   <!-- focused -->
    <item android:color="@color/GREY"/>     <!-- default -->
</selector>

绘制:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/edittext_shape" >

    <!-- <solid android:color="#ffffff" /> -->

    <gradient
        android:startColor="#FFFF0000"
        android:endColor="#FFFFFFFF"
        android:angle="270"
        android:centerX="0.25" />
    <corners
        android:bottomLeftRadius="6dp"
        android:bottomRightRadius="6dp"
        android:topLeftRadius="6dp"
        android:topRightRadius="6dp" />
    <padding
        android:left="7dp"
        android:right="7dp"
        android:bottom="5dp"
        android:top="5dp" />

    <stroke 
        android:width="7dp"
        android:color="@color/edit_text_color_state" />
</shape>

1 个答案:

答案 0 :(得分:0)

所以在经历了一些乱七八糟的事情之后,我终于通过在选择器中创建形状来实现这一点:

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

    <item android:state_pressed="true">
        <shape>
            <solid android:color="#ffffff" />
            <corners
                android:bottomLeftRadius="2dp"
                android:bottomRightRadius="2dp"
                android:topLeftRadius="2dp"
                android:topRightRadius="2dp" />
            <padding
                android:left="7dp"
                android:right="7dp"
                android:bottom="5dp"
                android:top="5dp" />

            <stroke 
                android:width="2dp"
                android:color="@color/ORANGE" />
        </shape>
    </item>

    <item android:state_focused="true">
        <shape>
            <solid android:color="#ffffff" />
            <corners
                android:bottomLeftRadius="2dp"
                android:bottomRightRadius="2dp"
                android:topLeftRadius="2dp"
                android:topRightRadius="2dp" />
            <padding
                android:left="7dp"
                android:right="7dp"
                android:bottom="5dp"
                android:top="5dp" />
            <stroke 
                android:width="2dp"
                android:color="@color/LIGHTBLUE" />
        </shape>
    </item>    

    <item>
        <shape>
            <solid android:color="#ffffff" />
            <corners
                android:bottomLeftRadius="2dp"
                android:bottomRightRadius="2dp"
                android:topLeftRadius="2dp"
                android:topRightRadius="2dp" />
            <padding
                android:left="7dp"
                android:right="7dp"
                android:bottom="5dp"
                android:top="5dp" />

            <stroke 
                android:width="2dp"
                android:color="@color/GREY" />
        </shape>
    </item>
</selector>

这意味着不必要地重复代码,因为我想要改变的只是笔触颜色,但它有效。