【问题标题】:CheckBox colour not changing复选框颜色不变
【发布时间】:2017-11-14 09:53:03
【问题描述】:

我有一个 CheckBox,但我不知道如何更改它的颜色。 我尝试在 styles.xml 和 styles21.xml 中创建自定义样式 我在那里添加了:

<style name="checkBoxStyle" parent="Base.Theme.AppCompat">
    <item name="colorAccent">@color/white100</item>
    <item name="android:textColorSecondary">@color/white87</item>
</style>

然后我将样式添加到我的 CheckBox 中,如下所示:

@style/checkBoxStyle

颜色没有变化。它仍然是黑色的,我想要它是白色的。 尝试创建另一个复选框,因为我教过我可能弄乱了前一个复选框。结果一样。

清单:

<?xml version="1.0" encoding="utf-8"?>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".InputTaskActivity"
        android:windowSoftInputMode="stateVisible">

    </activity>
</application>

带有复选框的 XML:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="55dp"
android:layout_margin="5dp"
android:background="@color/colorPrimaryDark"
android:orientation="vertical"
android:padding="10dp">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="horizontal"
    android:weightSum="1">

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:text="TextView"
        android:textColor="@color/white100"
        android:textSize="18sp" />

    <Space
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <CheckBox
        android:id="@+id/checkBox3"
        style="@style/checkBoxStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

</LinearLayout>

【问题讨论】:

  • 未选中时,我们只有一个框,我想要白色87,选中时,我希望框和里面的勾号是白色100;
  • 检查我下面的回答是否有帮助。

标签: android checkbox colors styles


【解决方案1】:

将style属性改为theme如下:

<CheckBox
    android:id="@+id/checkBox3"
    android:theme="@style/checkBoxStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1" />

【讨论】:

  • 有效!我接受了你的回答。我还有一个小问题。当按钮处于活动状态(选中复选框)时如何更改颜色?
  • @VladSkurtolov 更新了答案!试试看:-)
【解决方案2】:

您可能必须使用新的强调色创建一个新主题。然后,您必须在清单中为您的复选框所在的活动指定新创建的主题。

【讨论】:

    【解决方案3】:

    res/values/styles 中的 CheckBox 样式

    <style name="MyCheckBox" parent="Theme.AppCompat.Light">
        <item name="colorControlNormal">@color/colorGreen</item>
        <item name="colorControlActivated">@color/colorBlue</item>
    </style>
    

    res/values/colors.xml

    <color name="colorBlue">#022ce6</color>
    <color name="colorGreen">#02e602</color>
    

    在您的活动 XML 文件中

    <CheckBox
        android:id="@+id/checkBox3"
        android:theme="@style/MyCheckBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-24
      • 1970-01-01
      相关资源
      最近更新 更多