【问题标题】:customize check box preference自定义复选框首选项
【发布时间】:2011-04-03 22:06:49
【问题描述】:

我无法自定义我的复选框,虽然我已经在 xml 首选项文件中定义了背景,但它不会拉取文件。 1. 我正在尝试为复选框显示自定义图像,并将选择器 xml 定义为“android_button.xml”,如下所示:

   <?xml version="1.0" encoding="utf-8"?>
  <selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checkable="true"
      android:drawable="@drawable/state_normal" /> <!-- pressed -->
<item  android:state_checked="true"
      android:drawable="@drawable/android_pressed" /> <!-- focused -->
<item android:drawable="@drawable/state_normal" /> <!-- default -->
</selector>

state_normal 和 android_pressed 是 res>drawable 文件夹中的两个 .png 图像。

2.my Checkbox preference.xml 文件是:

          <CheckBoxPreference android:key="@string/Drop_Option"
            android:title="Close after call drop"
            android:defaultValue="true"
            android:background="@drawable/android_button"
            />

定义中是否有任何错误,屏幕中显示的唯一更改是 android:title 文本,如果我更改文本,它会更改文本。没有其他变化。我该如何解决。谢谢您的建议。

【问题讨论】:

    标签: android android-emulator android-manifest


    【解决方案1】:

    有两种方法可以实现你所需要的,第一种是在res/layout定义自定义复选框布局custom_chexbox.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/android_button"/>
    

    那么你需要为偏好指定这个布局:

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

    第二种方法是创建自定义主题,重新定义复选框视图的样式并将主题应用于首选项活动,详情请参阅How to customize the color of the CheckMark color in android in a dialog. : android

    【讨论】:

    • 病态,对 android:widgetLayout 毫无头绪,希望其他所有组件都有。
    • 这非常有效。你是救生员。我从来不知道我们也可以为小部件使用自定义布局。非常感谢。
    • 操作,我的错。显然,我使用了不同的 ID 名称。为这个极好的解决方案投一票!!
    • 另外,设置android:background="@null",使复选框点击真正透明(尤其是棒棒糖的涟漪效果)
    • 一开始我并没有特别注意的是ID。按照此处的答案设置 ID 非常重要,否则当您单击元素中的其他任何位置时,您将看不到字段更新!
    【解决方案2】:

    制作一个可drwable xml文件:

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

    以编程方式设置它 cb.setButtonDrawable(R.drawable.checkboxcustom);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-30
      • 1970-01-01
      相关资源
      最近更新 更多