如何在状态更改时更改Android切换按钮的文本颜色?

时间:2011-08-17 16:54:37

标签: android text colors togglebutton

我的toggle-Button为每个州(红色和白色)提供不同颜色的背景。 现在我需要在激活时更改togglebutton文本(红色/白色)的颜色。 使用xml我无法让它工作,也许任何人都知道我做错了什么?

布局xml中的我的按钮:

<ToggleButton 
android:paddingRight="20dip" 
android:id="@+id/pseudo_tab_right" 
android:layout_weight=".50" 
android:layout_width="wrap_content" 
android:textStyle="bold" 
android:paddingLeft="10dip" 
android:textSize="12sp" 
android:layout_height="wrap_content" 
android:textColor="@drawable/pseudo_tab_text_color"
android:textOff="@string/pseudo_tab_right_text"
android:textOn="@string/pseudo_tab_right_text"
android:background="@drawable/tab_button_right" 
/>  

按钮状态的xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@drawable/sort_button_red_right_43" /> 
<item android:drawable="@drawable/sort_button_white_right_43" />
</selector>

xml for color:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- pressed -->
<item android:state_pressed="true" android:color="#4f5459" /> 

<!-- focused -->
<item android:state_focused="true" android:color="#4f5459" />

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

<!-- trying these out, but none works -->
<item android:state_checked="true" android:color="#ff0000" />
<item android:state_enabled="true" android:color="#ff00dd" />
<item android:state_selected="true" android:color="#ff00dd" />
<item android:state_active="true" android:color="#ff00dd" />

</selector>

2 个答案:

答案 0 :(得分:15)

找到它: 现在我正在使用android:state_checked="true"android:state_checked="false"

颜色的xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:color="#ffffff" />
    <item android:state_checked="false" android:color="#000000" />
</selector>

答案 1 :(得分:-1)

您已使用以下示例,移动颜色属性以包含在项目标记中。

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

<item android:state_pressed="true" >
    <shape>
        <corners
            android:topLeftRadius="3dp"
            android:bottomLeftRadius="0.1dp"
            android:topRightRadius="0.1dp"
            android:bottomRightRadius="3dp" />
        <gradient
            android:startColor="#d0785e"
            android:endColor="#000000"
            android:angle="270" />
    </shape>
</item>

<item android:state_focused="true" >
    <shape>
        <corners
            android:topLeftRadius="3dp"
            android:bottomLeftRadius="0.1dp"
            android:topRightRadius="0.1dp"
            android:bottomRightRadius="3dp" />
        <gradient
            android:endColor="#ffffff"
            android:startColor="#b9b9b9"
            android:angle="270" />
    </shape>
</item>

<item android:state_enabled="false">
    <shape>
        <corners
            android:topLeftRadius="3dp"
            android:bottomLeftRadius="0.1dp"
            android:topRightRadius="0.1dp"
            android:bottomRightRadius="3dp" />
        <gradient
            android:startColor="#f0aa9f"
            android:endColor="#e21f00"
            android:angle="270" />
    </shape>
</item>

<item>        
    <shape>
        <corners
            android:topLeftRadius="3dp"
            android:bottomLeftRadius="0.1dp"
            android:topRightRadius="0.1dp"
            android:bottomRightRadius="3dp" />
        <gradient
            android:startColor="#fe9c69"
            android:endColor="#fc5700"
            android:angle="270" />
    </shape>
</item>


</selector>
相关问题