迁移到AndroidX之后,Lollipop下的API不支持android:button [英] After migration to AndroidX android:button is not respected for API below Lollipop

查看:138
本文介绍了迁移到AndroidX之后,Lollipop下的API不支持android:button的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的复选框:

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.

迁移到AndroidX后,我在API 21以下的设备上看到Android的默认映像。

After migration to AndroidX I see default image for Android on devices below API 21.

我已经尝试过的操作:


  • 将CheckBox更改为AppCompatCheckbox(来自AndroidX)-未更改

  • 将背景设置为复选框&设置 android:button = @ null -背景还可以,但是我仍然在背景上看到默认图片(请参见下面的图片)

  • 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)

Android似乎完全不尊重 button 属性。

Seems that Android completly disrespect button attribute.

< a href = https://i.stack.imgur.com/ga7n7.png rel = nofollow noreferrer>

我没主意了。对于Lollipop +,应有尽有。有没有人遇到过这样的问题?
我所做的唯一更改是迁移到AndroidX:/

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

推荐答案

在appcompat主题中,API 21下面的checkBoxStyle是定义为

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>

attr buttonCompat具有默认值来显示单击动画。

the attr buttonCompat has a default value to show the click animation. The attr buttonCompat take effect and ignore the button attr.

要修复此问题,必须对attr buttonCompat进行定义。在您的主题中添加

To fix it, the attr buttonCompat must be undefined. In your theme add

    <item name="checkboxStyle">@style/MyCheckBox</item>

并添加样式

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

也请在您的values-v21目录中,将其添加到主题中

Also in your values-v21 dir, add this to your theme

    <item name="checkboxStyle">?android:attr/checkboxStyle</item>

这篇关于迁移到AndroidX之后,Lollipop下的API不支持android:button的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆