Android 自定义后的材质复选框样式

Android 自定义后的材质复选框样式,android,android-custom-view,androidx,material-components,material-components-android,Android,Android Custom View,Androidx,Material Components,Material Components Android,我的自定义复选框(MyCheckbox)已从androidx.appcompat.widget.AppCompatCheckBox扩展,但默认样式不适用于它 <style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar"> ... <item name="android:checkboxStyle">@style/CheckboxTheme</item>

我的自定义复选框(
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>
并使用
@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"
             />

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

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

class MyCheckbox @JvmOverloads constructor(context: Context,
                                        attrs: AttributeSet? = null,
                                        defStyle: Int = 0)
: MaterialCheckBox(ContextThemeWrapper(context, R.style.CheckboxTheme), attrs, defStyle)
<style name="MyCheckBox" parent="@style/Widget.MaterialComponents.CompoundButton.CheckBox">
   <item name="materialThemeOverlay">@style/ThemeOverlay.CheckBox</item>
</style>

材质组件库提供的
MaterialCheckBox
使用主题中定义的
checkboxStyle
属性。
只需覆盖此属性即可全局定义应用程序中的样式:

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

只是一张便条。

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

您能否确认
com.google.android.material:material:1.2.0-alpha03
中存在此问题?您确定吗?我刚刚测试了它,视图的背景是新的原色。甚至不需要在xml布局中添加样式标记。也许您可以在v21中添加这种样式,并在apiNo上进行测试。这是旧版本上的一个bug,已在版本
1.2.0-alpha03
中修复。感谢@sinadarvishi使用您提供的精确代码,包括全局和单个解决方案,但选中的代码是灰色的!+通过使用
按钮nt
enabled=“false”
不会影响复选框本身;只在文字上;使其变为灰色或正常。
<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"
    ..>
<com.google.android.material.checkbox.MaterialCheckBox
    ...
    android:theme="@style/ThemeOverlay.CheckBox"/>