【问题标题】:PreferencesActivity - customize style of checbkoxes, radio buttons etc [duplicate]PreferencesActivity - 自定义 checbkoxes、单选按钮等的样式 [重复]
【发布时间】:2012-08-02 19:43:15
【问题描述】:

可能重复:
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>

【问题讨论】:

    标签: android checkbox preferences radio preferencescreen


    【解决方案1】:

    我明白你现在在说什么。我以为你只是在你的View 中使用普通的CheckBox。您正在使用 CheckBoxPreference 和另一个 Preference Views

    好的,这个概念非常相似。

    您需要定义自定义小部件布局。在您的 res/layout 目录中执行此操作并将其命名为 custom_checkbox.xml。我们可以以CheckBox 为例。

    使用CheckBox 定义您希望CheckBoxPreference 与您在res/layout 目录中创建的自定义小部件布局的外观。例如:

    <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 设置为您刚刚在res/layout 中定义的自定义CheckBox。例如:

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

    康斯坦丁·布罗夫


    来自 cmets 的扩展问题的相关链接:

    另外,如何改变列表项的样式?

    android-listview-style

    另外,我想为列表项添加一些填充。

    spacing-between-listview-items-android

    【讨论】:

    • 事实并非如此。我需要将它们添加到 CheckBoxPreference、ListPreference 等。你的方法没问题,但它不适用于那些。没有背景属性。
    • 我想念你的问题。请阅读我的编辑。我现在明白你说的了。一旦您确认此答案正确,我将删除之前的答案。
    • 好吧,这看起来很有希望 :) 我找不到 CheckBoxPreferences 的 xml 和 custom_checkbox.xml -> android:widgetLayout="@layout/custom_checkbox" 之间的链接。下班后我会测试一下。
    • 让我知道它是否适合您。
    • 是的,它正在工作,但我遇到了另一个问题。现在,不是整个条目(带有名称、摘要等)是可点击的,但只有复选框图像:/ 另外,如何更改列表项的样式?我想要列表项的窗口背景(图像)和白色背景。有机会做吗?另外,我想在列表项中添加这样的填充。我不确定这是否可能。也许覆盖整个 PreferencesActivity?
    猜你喜欢
    • 2015-10-04
    • 2014-01-22
    • 2016-11-17
    • 2022-06-15
    • 1970-01-01
    • 1970-01-01
    • 2019-11-18
    • 2012-05-23
    • 1970-01-01
    相关资源
    最近更新 更多