customize check box preference

前端 未结 2 1311
天命终不由人
天命终不由人 2020-11-27 05:45

I am unable to customize my checkbox , although I have defined the background in the xml preference file, it doesn\'t pull the file. 1. I am trying to display custom images

相关标签:
2条回答
  • 2020-11-27 06:03

    There are two ways to achieve what you need, first is to define custom checkbox layout custom_chexbox.xml at res/layout:

    <?xml version="1.0" encoding="UTF-8"?>
    <CheckBox xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+android:id/checkbox" android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:focusable="false"
    android:clickable="false" android:button="@drawable/android_button"/>
    

    Then you need to specify this layout for the preference:

    <CheckBoxPreference android:key="@string/Drop_Option"
     android:title="Close after call drop" android:defaultValue="true"
     android:widgetLayout="@layout/custom_checkbox"/>
    

    Second way is to create a custom theme, redefine style for checkbox views and apply the theme to the preferences activity, see How to customize the color of the CheckMark color in android in a dialog. : android for details.

    0 讨论(0)
  • 2020-11-27 06:25

    Make one drwable xml file :

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    
        <item android:drawable="@drawable/checkbox_active_btn" android:state_checked="true"></item>
        <item android:drawable="@drawable/checkbox_active_btn" android:state_checked="true" android:state_enabled="false" android:state_focused="true"></item>
        <item android:drawable="@drawable/checkbox_active_btn" android:state_checked="true" android:state_enabled="false"></item>
        <item android:drawable="@drawable/checkbox_active_btn" android:state_checked="true" android:state_focused="true"></item>
        <item android:drawable="@drawable/checkbox_active_btn" android:state_checked="true" android:state_pressed="true"></item>
        <item android:drawable="@drawable/checkbox_inactive_btn" android:state_checked="false"></item>
        <item android:drawable="@drawable/checkbox_inactive_btn" android:state_checked="false" android:state_enabled="false" android:state_focused="true"></item>
        <item android:drawable="@drawable/checkbox_inactive_btn" android:state_checked="false" android:state_enabled="false"></item>
        <item android:drawable="@drawable/checkbox_inactive_btn" android:state_checked="false" android:state_focused="true"></item>
        <item android:drawable="@drawable/checkbox_inactive_btn" android:state_checked="false" android:state_pressed="true"></item>
    
    </selector>
    

    Set it programatically by cb.setButtonDrawable(R.drawable.checkboxcustom);

    0 讨论(0)
提交回复
热议问题