Possible Duplicate:
Change icons of checked and unchecked for Checkbox for Android
customize check box preference
有没有办法自定义PreferencesActivity
中元素的样式?
我只能更改颜色文本、背景等。
我还想更改 CheckBoxPreference
中的复选标记和单选按钮样式等等。
我对 API8 或更高版本感兴趣。
感谢您的帮助!
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<CheckBoxPreference
android:defaultValue="true"
android:key="vibrate"
android:summary="summary"
android:title="Some title"
android:widgetLayout="@layout/custom_checkbox" />
<CheckBoxPreference
android:defaultValue="true"
android:key="autorotate"
android:summary="summary"
android:title="auto rotate" />
</PreferenceScreen>
最佳答案
我现在明白你在说什么了。我以为你只是使用正常的 CheckBox
在你的View
里面。您正在使用CheckBoxPreference
另一个Preference
Views
.
好吧,这个概念非常相似。
您需要定义自定义小部件布局。在您的 res/layout
中执行此操作目录并将其命名为 custom_checkbox.xml
。我们可以使用CheckBox
举个例子。
定义您想要的 CheckBoxPreference
查看您在 res/layout
中创建的自定义小部件布局使用CheckBox
目录。例如:
<CheckBox
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+android:id/customCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@drawable/android_button"
android:clickable="false"
android:focusable="false" />
您可以替换 android:button
与任何drawable
您发现或设计的。
现在您需要定义您的 CheckBoxPreference
并设置其 android:widgetLayout
来定制CheckBox
您刚刚在 res/layout
中定义。例如:
<CheckBoxPreference
android:defaultValue="true"
android:key="@string/Drop_Option"
android:title="Close after call drop"
android:widgetLayout="@layout/custom_checkbox" />
如果您没有使用xml
用于定义您的layout
你可以用这样的代码来做到这一点:
CheckBoxPreference checkBoxPreference = new CheckBoxPreference(this);
checkBoxPreference.setWidgetLayoutResource(R.layout.custom_checkbox);
Resources found that were similar to this question.
康斯坦丁·布罗夫
评论中扩展问题的相关链接:
Also, how to change style of list item?
Also, I would like to add some paddings to list items.
关于android - PreferencesActivity - 自定义复选框、单选按钮等的样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11818076/