android - PreferencesActivity - 自定义复选框、单选按钮等的样式

标签 android checkbox radio-button preferences preferencescreen

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?

android-listview-style

Also, I would like to add some paddings to list items.

spacing-between-listview-items-android

关于android - PreferencesActivity - 自定义复选框、单选按钮等的样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11818076/

相关文章:

android - 将图像放在坐标 Android

java - Firebase 数据库无法正常工作且无响应

jquery - symfony 表单在输入类型 ="choice"上使用 jquery 更改方法

javascript:如果选择了单选按钮,则在提交时等待 2 秒,然后显示警报

reactjs - React UseState boolean 数组修改一个元素

c# - 单选按钮列表文本不会一直向右对齐 ASP.NET C#

android - 使用 IBM Watson Android sdk 将音频文件写入 websocket

android - 如何从 Android Studio 中的 APK 构建中排除默认资源?

php - 复选框和 MySQL

jquery - 这可能是带有自动完成和复选框的下拉列表吗?