Android 自定义复选框首选项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3569412/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
customize check box preference
提问by David Prun
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 首选项文件中定义了背景,但它并没有提取文件。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 and android_pressed are two .png images in res>drawable folder.
state_normal 和 android_pressed 是 res>drawable 文件夹中的两个 .png 图像。
2.my Checkbox preference.xml file is :
2.我的复选框首选项.xml文件是:
<CheckBoxPreference android:key="@string/Drop_Option"
android:title="Close after call drop"
android:defaultValue="true"
android:background="@drawable/android_button"
/>
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.
定义中是否有任何错误,屏幕中显示的唯一更改是 android:title 文本,如果我更改文本,它会更改文本。没有其他变化。我该如何解决。谢谢你的建议。
回答by Konstantin Burov
There are two ways to achieve what you need, first is to define custom checkbox layout custom_chexbox.xml at res/layout:
有两种方法可以实现您的需求,第一种是在 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"/>
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. : androidfor details.
第二种方法是创建自定义主题,重新定义复选框视图的样式并将主题应用于首选项活动,请参阅如何在对话框中自定义 android 中的 CheckMark 颜色的颜色。: 安卓了解详情。
回答by Gevaria Purva
Make one drwable xml file :
制作一个可绘制的 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>
Set it programatically by cb.setButtonDrawable(R.drawable.checkboxcustom);
通过 cb.setButtonDrawable(R.drawable.checkboxcustom); 以编程方式设置它;