问题
I have very simple checkbox:
<CheckBox xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/clipboardBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@drawable/clipboard_checkbox" />
For checked/unchecked I have two different images.
After migration to AndroidX I see default image for Android on devices below API 21.
What I've tried already:
- change CheckBox to AppCompatCheckbox (from AndroidX) - nothing changed
- set background to checkbox & set
android:button="@null"
- background is OK, but I still see default image on background (see image below)
Seems that Android completly disrespect button
attribute.
I am out of ideas. For Lollipop+ everythink works as it should. Have anyone faced issue like this? Only change I did was migration to AndroidX :/
回答1:
In the appcompat theme, the checkBoxStyle below API 21 is defined as
<style name="Base.Widget.AppCompat.CompoundButton.CheckBox" parent="android:Widget.CompoundButton.CheckBox">
<item name="android:button">?android:attr/listChoiceIndicatorMultiple</item>
<item name="buttonCompat">?attr/listChoiceIndicatorMultipleAnimated</item>
<item name="android:background">?attr/controlBackground</item>
</style>
the attr buttonCompat has a default value to show the click animation. The attr buttonCompat take effect and ignore the button attr.
To fix it, the attr buttonCompat must be undefined. In your theme add
<item name="checkboxStyle">@style/MyCheckBox</item>
and add a style
<style name="MyCheckBox" parent="android:Widget.CompoundButton.CheckBox">
<item name="android:button">?android:attr/listChoiceIndicatorMultiple</item>
<item name="buttonCompat">@null</item>
<item name="android:background">?attr/selectableItemBackgroundBorderless</item>
</style>
Also in your values-v21 dir, add this to your theme
<item name="checkboxStyle">?android:attr/checkboxStyle</item>
回答2:
You need to set the button and buttonCompat null for androidx libraries. It will look like below-
<androidx.appcompat.widget.AppCompatCheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@null"
app:buttonCompat="@null"
android:background="@drawable/cb_pause_resume_selector"
/>
来源:https://stackoverflow.com/questions/55631014/after-migration-to-androidx-androidbutton-is-not-respected-for-api-below-lollip