自定义后的材料复选框样式 [英] material checkbox style after customize

本文介绍了自定义后的材料复选框样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My customized checkbox (MyCheckbox) has extended from androidx.appcompat.widget.AppCompatCheckBox, but the default styles don't apply to it.

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

但未在预览和运行时更改任何内容.

but did not change anything in preview and runtime.

我还迁移了MaterialComponent(从com.google.android.material.checkbox.MaterialCheckBox扩展)版本:

Also, I migrated on MaterialComponent (extending from com.google.android.material.checkbox.MaterialCheckBox) version:

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

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

and using @style/Widget.MaterialComponents.CompoundButton.CheckBox for style. but DIDN'T change.

注意: 仅当我提到每个实例的样式时,此问题才解决:

NOTE: This issue fixes just when I mentioned the style for each instance:

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

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

推荐答案

材料组件库提供的MaterialCheckBox使用主题中定义的checkboxStyle属性.
只需覆盖此属性即可在您的应用中全局定义样式:

The MaterialCheckBox provided by the Material Components library uses the checkboxStyle attribute defined in the theme.
Just override this attribute to define globally the style in your app:

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

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

If you want to customize the color you can use the materialThemeOverlay attribute in the style:

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

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

You can also apply the style to the single checkbox using:

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

您也可以在布局中使用android:theme,但它不能在全局范围内使用.

As alternative you can also use the android:theme in the layout, but it doesn't work globally.

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

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

Just a note.
The colorTint selector is defined programmatically in the MaterialCheckBox code. You can also define a custom colorTint selector adding the buttonTint attribute in your custom style. In this case the colors defined above are ignored.

这篇关于自定义后的材料复选框样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆