【问题标题】:How to set custom layout on CheckBox preferences in Android?如何在 Android 中的 CheckBox 首选项上设置自定义布局?
【发布时间】:2014-02-14 05:17:59
【问题描述】:

我开发了一个应用程序,它有一个用于应用程序设置的首选项活动,所以我想知道我们是否可以在首选项布局上设置自定义图形布局 (XML),例如 CheckBox 在选中和未选中时有自己的颜色按钮,如果可能的话,列表有自己的颜色那怎么办?

【问题讨论】:

    标签: android android-layout sharedpreferences android-preferences


    【解决方案1】:

    使用 button.xml 在 Drawable 中创建 XML

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

    radio_on

    radio_off

    将两个图像放入可绘制文件夹radio_on.png 和radio_off.png

    现在在你的单选按钮上写这个

    android:button="@drawable/button"
    

    【讨论】:

      【解决方案2】:

      custom_checkbox_layout.xml

      <?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/button"/>
      

      之后在 CheckBoxPreference 中设置 android:widgetLayout="@layout/custom_checkbox_layout"

      【讨论】:

        猜你喜欢
        • 2018-06-23
        • 1970-01-01
        • 2017-01-06
        • 1970-01-01
        • 1970-01-01
        • 2011-09-05
        • 2020-10-15
        • 1970-01-01
        相关资源
        最近更新 更多