【问题标题】:material checkbox style after customize自定义后的材质复选框样式
【发布时间】:2020-04-18 13:59:26
【问题描述】:

我的自定义复选框 (MyCheckbox) 已从 androidx.appcompat.widget.AppCompatCheckBox 扩展,但默认样式不适用于它。

<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
    ...
    <item name="android:checkboxStyle">@style/CheckboxTheme</item>
    <item name="checkboxStyle">@style/CheckboxTheme</item>
</style>

<style name="CheckboxTheme" parent="@style/Widget.AppCompat.CompoundButton.CheckBox">
     <!-- for example try to change background -->
    <item name="android:background">@color/colorPrimary</item>
</style>

但在预览和运行时没有改变任何东西。

另外,我迁移到 MaterialComponent(从 com.google.android.material.checkbox.MaterialCheckBox 扩展)版本:

com.google.android.material:material:1.1.0-beta01

并使用@style/Widget.MaterialComponents.CompoundButton.CheckBox 作为样式。但没有改变。

注意: 当我提到每个实例的样式时,这个问题就解决了:

            <com.something.MyCheckBox
                android:id="@+id/ch"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="some thing"
                style="@style/CheckboxTheme"
             />

但我想为此类的所有实例设置此样式。 你能帮帮我吗?

【问题讨论】:

  • 你能确认com.google.android.material:material:1.2.0-alpha03中存在这个问题吗?
  • 你确定吗?我刚刚对其进行了测试,并且视图的背景新是原色。甚至不需要在 xml 布局中添加样式标签。也许您在 v21 中添加此样式并在 api 上进行测试
  • 没有。这是使用版本的一个错误,并在版本1.2.0-alpha03 中修复。谢谢@SinaDarvishi

标签: android android-custom-view androidx material-components material-components-android


【解决方案1】:

Material Components 库提供的MaterialCheckBox 使用了主题中定义的checkboxStyle 属性。
只需覆盖此属性即可在您的应用中全局定义样式:

<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
   <!-- .... -->
   <item name="checkboxStyle">@style/MyCheckBox</item>
</style>

如果要自定义颜色,可以在样式中使用materialThemeOverlay属性:

<style name="MyCheckBox" parent="@style/Widget.MaterialComponents.CompoundButton.CheckBox">
   <item name="materialThemeOverlay">@style/ThemeOverlay.CheckBox</item>
</style>

与:

  <style name="ThemeOverlay.CheckBox" parent="">
    <item name="colorSecondary">@color/....</item>   <!-- checked -->
    <item name="colorOnSurface">@color/.....</item>  <!-- unchecked -->
  </style>

您还可以使用以下方法将样式应用于单个复选框:

<com.google.android.material.checkbox.MaterialCheckBox
    style="@style/MyCheckBox"
    ..>

您也可以在布局中使用android:theme,但它不适用于全局。

<com.google.android.material.checkbox.MaterialCheckBox
    ...
    android:theme="@style/ThemeOverlay.CheckBox"/>

只是一个注释。
colorTint 选择器在MaterialCheckBox 代码中以编程方式定义。您还可以定义自定义 colorTint 选择器,在自定义样式中添加 buttonTint 属性。在这种情况下,上面定义的颜色将被忽略。

【讨论】:

  • 我使用您提供的确切代码,包括全局和单一解决方案,但是选中的代码是灰色的! + 并且通过使用buttonTint,enabled="false" 不会影响复选框本身;仅在文字上;使其变为灰色或正常。
【解决方案2】:

根据this answer,我尝试使用ContextThemeWrapper来应用样式:

class MyCheckbox @JvmOverloads constructor(context: Context,
                                        attrs: AttributeSet? = null,
                                        defStyle: Int = 0)
: MaterialCheckBox(ContextThemeWrapper(context, R.style.CheckboxTheme), attrs, defStyle)

【讨论】:

    猜你喜欢
    • 2018-03-21
    • 1970-01-01
    • 2015-08-17
    • 2019-07-03
    • 1970-01-01
    • 2022-07-19
    • 1970-01-01
    • 2015-03-21
    相关资源
    最近更新 更多