material checkbox style after customize

落爺英雄遲暮 提交于 2020-05-23 21:05:25

问题


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.

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

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

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

but I want to set this style for all instances of this class. Could you help me?


回答1:


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>

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>

with:

  <style name="ThemeOverlay.CheckBox" parent="">
    <item name="colorSurface">@color/....</item>
    <item name="colorOnSurface">@color/.....</item>
  </style>

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.

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



回答2:


According to this answer, I tried using ContextThemeWrapper to apply the style:

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


来源:https://stackoverflow.com/questions/59520384/material-checkbox-style-after-customize

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!