【问题标题】:Why does androidx.preference library stop my custom checkboxes from displaying properly?为什么 androidx.preference 库会阻止我的自定义复选框正确显示?
【发布时间】:2020-01-19 10:39:10
【问题描述】:

我决定将我的 Options 活动更新为 androidx.preference。所以我将implementation 'androidx.preference:preference-ktx:1.1.0' 添加到我的依赖项中。但是,现在我应用中其他地方的自定义复选框不再使用为它们提供的自定义按钮,而只使用默认复选框。(注意:这只发生在运行 Android 4.4 或更低版本的设备上)

删除 implementation 'androidx.preference:preference-ktx:1.1.0' 可解决此问题。有谁知道为什么会发生这种情况以及我如何克服它?


在活动布局中:

 <androidx.appcompat.widget.AppCompatCheckBox
        android:id="@+id/cbSharePosition"
        style="@style/ActionCenter.Light.CheckBox"
        android:layout_width="wrap_content"
        android:layout_height="45dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="16dp"
        android:checked="true"/>

在 style_widgets.xml 中:

<style name="ActionCenter.Light.CheckBox" parent="android:style/Widget.CompoundButton.CheckBox">
    <item name="android:button">@drawable/actioncenter_switch_button</item>
    <item name="android:background">@color/transparent</item>
    <item name="android:scaleType">centerInside</item>
    <item name="android:adjustViewBounds">true</item>
</style>

actioncenter_switch_button.xml:

   <selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:drawable="@drawable/switch_on"
   android:state_checked="true" />

   <item android:drawable="@drawable/switch_off"
   android:state_checked="false" />

   <item android:drawable="@drawable/switch_on" />
   </selector>

【问题讨论】:

  • 也许preferences-ktx 中的一个扩展函数会影响你自己的东西。如果你使用标准的 java 依赖而没有 ktx 怎么办?
  • @Tenfour04 结果相同:(

标签: android user-interface gradle kotlin androidx


【解决方案1】:

这似乎是Appcompat 库的1.1.0 版本中的问题。 您可以使用 buttonCompat 属性而不是 button 并且一切都应该正常工作。

你可以替换:

<item name="android:button">@drawable/actioncenter_switch_button</item>

与:

<item name="buttonCompat">@drawable/actioncenter_switch_button</item>

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-16
    • 2020-05-04
    • 2018-05-06
    • 1970-01-01
    相关资源
    最近更新 更多