2015-02-24 68 views
0

我已经使用开关按钮,当应用程序在4.2及以上版本上运行时,一切正常,但文本颜色在4.0.4中完全没有改变为白色我尝试了所有可能的解决方案Android开关按钮颜色在4.0.4中没有变化

我的开关:

<Switch 
     android:layout_width="100dp" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/facilityassetdescription" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="7dp" 
     android:background="@drawable/offbuttonbg" 
     android:textColor="@style/toggle_text" 
     android:textOff="OFF" 
     android:textOn="ON" 
     android:thumb="@drawable/switchselector" /> 

我的风格文件

<style name="toggle_text"> 
     <item name="android:textColor">@color/toggle</item> 
    </style> 

RES /颜色/ toggle_text.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"/> 
    <!-- Default State --> 
    <item android:color="#ffffff"/> 

</selector> 

请给任何想法

+0

这两种颜色都是白色的?你是否通过改变其中一种颜色来检查? – 2015-02-24 09:51:40

+0

这里都显示黑色。而不是白 – Madhu 2015-02-24 09:53:00

+0

尝试给文本颜色属性一个可绘制的,而不是一种颜色。将toggle_text.xml放在drawable文件夹中,并设置textColor =“@ drawable/toggle_text”并查看它是否有效。 – 2015-02-24 09:58:29

回答

2

使用下面的风格来摆脱这个问题的:

<style name="toggle_text" parent="@android:style/TextAppearance.Small"> 
    <item name="android:textColor">@color/toggle</item> 
</style> 

,并在对开关提到Android的xml文件:switchTextAppearance属性而不是采用了android:文字颜色:

<Switch 
    android:layout_width="100dp" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/facilityassetdescription" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="7dp" 
    android:background="@drawable/offbuttonbg" 
    android:switchTextAppearance="@style/toggle_text" 
    android:textOff="OFF" 
    android:textOn="ON" 
    android:thumb="@drawable/switchselector" /> 
+0

它不工作 – Madhu 2015-02-24 10:51:57

+0

我编辑了我的answer.once试试。 – Ramesh 2015-02-24 10:54:49

+0

伟大的,它的工作。 android:switchTextAppearance =“@ style/toggle_text”答案是用这行代码对吗? – Madhu 2015-02-24 11:01:33

1

使用支持7 SwitchCompat

,并把<item name="colorSwitchThumbNormal">#E21D1D</item>在你的风格 和<item name="colorSwitchThumbNormal">#E21D1D</item>在你的主题

<android.support.v7.widget.SwitchCompat 
        android:id="@+id/switch_options" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignParentRight="true" 
        android:layout_centerVertical="true" 
        android:padding="5dp" 
        app:showText="false" > 
       </android.support.v7.widget.SwitchCompat> 

GUD LUCK !!

6

您可以使用开关COMPAT使用支持V7 library.Try新概念

my.xml

<android.support.v7.widget.SwitchCompat 
    android:id="@+id/sc_push" 
    style="@style/switchStyle" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true" 
    android:layout_centerVertical="true" 
    android:theme="@style/switchStyle" 
    app:theme="@style/switchStyle" /> 

style.xml

<style name="switchStyle"> 
    <item name="colorControlActivated">@color/red</item> 
    <item name="android:colorForeground">@color/gray</item> 
</style> 
打算交换机

祝你好运