【问题标题】:Android - How combining Shape drwable and text color different states for button?Android - 如何为按钮组合形状可绘制和文本颜色的不同状态?
【发布时间】:2011-10-13 15:06:45
【问题描述】:

我在管理按钮的 Android 状态列表时遇到了一些麻烦。我为不同的状态指定了一些 Shape drwable 项目,但我还需要根据当前状态更改 textColor。

我的实际状态列表xml是:

<?xml version="1.0" encoding="utf-8"?>    
<selector
    xmlns:android="http://schemas.android.com/apk/res/android">
 <item android:state_pressed="true">
        <shape>
            <gradient
                android:startColor="@color/white"
                android:endColor="@color/light_gray"
                android:angle="270" />
            <stroke
                android:width="3dp"
                android:color="@color/classic_red1" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>

    <item>
        <shape>
            <gradient
                android:startColor="@color/classic_red1"
                android:endColor="@color/classic_red2"
                android:angle="270" />
            <stroke
                android:width="2dp"
                android:color="@color/white" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
</selector>

我还需要根据这两种状态更改 textColor。 提前致谢。

【问题讨论】:

    标签: android drawable shape textcolor


    【解决方案1】:

    按钮 BG 的选择器:

     <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape>
            <gradient
                android:startColor="@color/white"
                android:endColor="@color/light_gray"
                android:angle="270" />
            <stroke
                android:width="3dp"
                android:color="@color/classic_red1" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    
    </item>
    
    <item android:state_focused="true">
        <shape>
            <solid android:color="#424242" />  //another custom shape here for focus state
        </shape>
    </item>
    
    <item>
        <shape>
            <gradient
                android:startColor="@color/classic_red1"
                android:endColor="@color/classic_red2"
                android:angle="270" />
            <stroke
                android:width="2dp"
                android:color="@color/white" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    
    </item>
    

    按钮文本颜色的选择器:

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_pressed="true" android:color="@color/white" /> <!-- pressed -->
        <item android:color="@color/black" /> <!-- default/unchecked -->
    </selector>
    

    【讨论】:

    • 啊啊!!真是个傻瓜..我想必须将所有内容都放在后台可绘制资源中。非常感谢!
    • 而BG选择器是drawable目录下的一个xml,其中文字颜色一是color目录下的一个xml——见stackoverflow.com/questions/8743584/…
    猜你喜欢
    • 2020-09-21
    • 1970-01-01
    • 1970-01-01
    • 2017-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-30
    相关资源
    最近更新 更多