After migration to AndroidX android:button is not respected for API below Lollipop

久未见 提交于 2019-12-02 11:24:08

问题


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

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