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

前端 未结 3 602
陌清茗
陌清茗 2021-01-23 06:32

I have very simple checkbox:



        
相关标签:
3条回答
  • 2021-01-23 07:07

    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>
    
    0 讨论(0)
  • 2021-01-23 07:10

    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"
                    />
    
    0 讨论(0)
  • 2021-01-23 07:23

    Change CheckBox to AppCompatCheckBox(AndroidX) and replace android:button to app:buttonCompat

      <androidx.appcompat.widget.AppCompatCheckBox
        android:id="@+id/clipboardBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/email_sign_in_button"
        app:buttonCompat="@drawable/clipboard_checkbox" />
    
    0 讨论(0)
提交回复
热议问题