【问题标题】:android - styling checkbox and radiobuttonandroid - 样式复选框和单选按钮
【发布时间】:2023-04-09 00:40:02
【问题描述】:

我正在尝试为我的应用设计样式并制作自己的主题,但我遇到了一些问题,因为我需要一些不寻常的行为。

我使用 Material Design 主题和父 Theme.AppCompat.Light.DarkActionBar

概述

视图有颜色:

  • 文本为textColorPrimarytextColorSecondarytextColorTertiary
  • 按钮是未知的灰色
  • 按钮文本是textColorPrimary (?)
  • 复选框和单选按钮未选中的边框是textColorSecondary
  • 复选框、单选按钮、滑块等颜色为colorAccent

但我需要

  • 按钮有 accentColor 背景和白色文本
  • 所有文字(textColorPrimarytextColorSecondarytextColorTertiary)都是深绿色的变体
  • 复选框、单选按钮在选中和取消选中时也应该是原色。

我的风格

按钮

所以,我创造了风格Button.Default

<style name="Button.Default" parent="Widget.AppCompat.Button">
    <item name="colorButtonNormal">@color/colorAccent</item>
    <item name="android:textColor">@android:color/white</item>
</style>

并在我的主题中使用它:

<item name="android:buttonStyle">@style/Button.Default</item>

好的,颜色改变了,但背景没有。我将这一行添加到主题样式中:

<item name="colorButtonNormal">@color/colorFakeAccent</item>

好的

复选框

<style name="Checkbox.Default" parent="Widget.AppCompat.CompoundButton.CheckBox">
    <item name="android:textColor">@color/colorPrimary</item>
    <item name="colorAccent">@color/colorPrimary</item>
</style>

并且复选框样式没有改变。如何设置复选框、单选按钮未选中和选中颜色?

【问题讨论】:

  • 谢谢,这对我有帮助。您可以将此作为答案发布,以标记它吗?

标签: android android-layout material-design


【解决方案1】:

使用&lt;item name="colorControlNormal"&gt; &lt;item name="colorControlActivated"&gt;改变复选框的颜色

【讨论】:

    【解决方案2】:

    您可以像这样添加自己的样式和图像

    <RadioButton
                android:id="@+id/my_rbutton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:button="@drawable/community_radiobutton_style" />
    

    custom_radio_button.xml

    <selector xmlns:android="http://schemas.android.com/apk/res/android" >
        <item android:state_checked="true" android:drawable="@drawable/ic_radio_on" />
        <item android:state_checked="false" android:drawable="@drawable/ic_radio_off" />
    </selector>
    

    【讨论】:

    • custom_radio_button.xml是如何使用的?
    【解决方案3】:

    您是否尝试在res/values-v21/styles.xml 文件中编写样式?

    【讨论】:

      猜你喜欢
      • 2017-01-07
      • 2015-12-09
      • 2012-01-22
      • 1970-01-01
      • 2010-11-10
      • 2019-04-28
      • 1970-01-01
      • 2011-10-18
      相关资源
      最近更新 更多