自定义复选框首选项 [英] customize check box preference

查看:30
本文介绍了自定义复选框首选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法自定义我的复选框,尽管我已经在 xml 首选项文件中定义了背景,但它并没有提取文件.1. 我正在尝试为复选框显示自定义图像,并将选择器 xml 定义为android_button.xml",如下所示:

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 for checkbox and have defined the selector xml as "android_button.xml" which looks like :

   <?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 图片.

state_normal and android_pressed are two .png images in res>drawable folder.

2.我的复选框首选项.xml文件是:

2.my Checkbox preference.xml file is :

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

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

Is there any error in the definition, The only change that shows up in screen is the android:title text, if I change the text, it changes the text. Nothing else changes. How do I fix this. Thank you for your suggestion.

推荐答案

有两种方法可以实现你所需要的,第一种是在 res/layout 定义自定义复选框布局 custom_chexbox.xml:

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

第二种方法是创建自定义主题,重新定义复选框视图的样式并将主题应用于首选项活动,请参阅如何在android对话框中自定义CheckMark颜色的颜色.:安卓了解详情.

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.

这篇关于自定义复选框首选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆