Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
884 views
in Technique[技术] by (71.8m points)

android - material checkbox style after customize

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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="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"
    ..>

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...