【问题标题】:Android Switch button color not changing in 4.0.4Android Switch 按钮颜色在 4.0.4 中没有改变
【发布时间】:2015-02-24 09:39:12
【问题描述】:

我使用过切换按钮,当应用程序在 4.2 及更高版本上运行时一切正常,但在 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/color/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>

请提供任何解决此问题的想法

【问题讨论】:

  • 两种颜色都是白色的?您是否通过更改其中一种颜色进行检查?
  • 这里都显示黑色。而不是白色
  • 尝试给文本颜色属性一个可绘制对象,而不是颜色。把toggle_text.xml放到drawable文件夹里,设置textColor="@drawable/toggle_text"看看能不能用。
  • 是的,让我检查一下
  • 你试过给 android:textColor="#FFFFFF" 吗?

标签: android button switch-statement textcolor


【解决方案1】:

您可以使用支持 v7 的 Switch 的 Switch-Compat 意图 图书馆。尝试新概念

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>

祝你好运

【讨论】:

    【解决方案2】:

    使用以下样式:

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

    并且,在开关的 xml 文件中提到 android:switchTextAppearance 属性而不是使用 android:textColor:

    <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" />
    

    【讨论】:

    • 很好,它的工作。 android:switchTextAppearance="@style/toggle_text" 答案就在这一行吧?
    【解决方案3】:

    使用支持 7 SwitchCompat

    并将&lt;item name="colorSwitchThumbNormal"&gt;#E21D1D&lt;/item&gt; 放入您的风格中 和&lt;item name="colorSwitchThumbNormal"&gt;#E21D1D&lt;/item&gt; 在您的主题中

    <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>
    

    好运!!

    【讨论】:

      【解决方案4】:

      如果上述答案不起作用,请尝试这个..

       <androidx.appcompat.widget.SwitchCompat
                      android:id="@+id/switch_is_in_stock"
                      style="@style/proximaNovaNormalColorBlack.size14"
                      android:layout_width="0dp"
                      android:layout_height="0dp"
                      android:maxLines="1"
                      android:padding="4dp"
                      android:textColor="@color/switch_thumb_selector"/>
      

      答案在这个 xml 行 -> android:textColor="@color/switch_thumb_selector"

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-10-04
        • 2015-10-29
        • 2016-11-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多