【问题标题】:Android: Creating custom preferenceAndroid:创建自定义首选项
【发布时间】:2013-04-13 01:58:34
【问题描述】:

是否可以在PreferenceScreen 中创建个人偏好?

我想这样编码颜色设置:

我知道使用ListPreference 可以很容易地选择颜色,但是使用那种“复选框”会很棒。

【问题讨论】:

    标签: android android-layout android-preferences preferenceactivity


    【解决方案1】:

    Android 开发者页面仅显示如何创建DialogFragment。不过,仍然可以自定义 Preference 项目的外观。在您的 XML 中,您必须将根元素声明为 android:id="@android:id/widget_frame,然后将 TextView 声明为 android:titleandroid:summary。然后,您可以声明要出现在布局中的其他元素。这是一个显示SeekBar 的示例,您可以轻松地将其适配为多复选框颜色选择器。

    seekbar_preference.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/widget_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <TextView
            android:id="@android:id/title"
            style="@android:style/TextAppearance.DeviceDefault.SearchResult.Title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Title" />
    
        <TextView
            android:id="@android:id/summary"
            style="@android:style/TextAppearance.DeviceDefault.SearchResult.Subtitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Summary" />
    
        <SeekBar
            android:id="@+id/seekbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    
    </LinearLayout>
    

    然后,在派生自 Preference 的类中,重写 onCreateView() 方法:

    SeekbarPreference.java

    @Override
    protected View onCreateView( ViewGroup parent )
    {
      LayoutInflater li = (LayoutInflater)getContext().getSystemService( Context.LAYOUT_INFLATER_SERVICE );
      return li.inflate( R.layout.seekbar_preference, parent, false);
    }
    

    然后在preferences.xml 文件中使用首选项:

    preferences.xml

    <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
    
        <com.example.SeekbarPreference
            android:key="pref_max_volume"
            android:title="@string/max_volume" />
        <com.example.SeekbarPreference
            android:key="pref_balance"
            android:title="@string/balance" />
    
    </PreferenceScreen>
    

    这给出了如下所示的首选项:

    您可以轻松调整此方法以在一行中显示多个复选框,就像在原始问题中一样。

    【讨论】:

    • +1 for
    • 我想知道如何在禁用颜色的情况下进行这样的自定义首选项.. 有人吗?
    • 请注意,SeekBarPreference 的布局实际上可以在这里找到:&lt;andorid-sdk&gt;/platforms/android-24/data/res/layout/preference_widget_seekbar.xml。 Android 布局不使用widget_frame id 作为根元素,你为什么说必须这样做?实际上,XML 中有一条注释说“Preference 应该将其实际的首选项小部件放在这里”。 标题和摘要TextViews 之后,但在LinearLayout 之前,ID 为“widget_frame”。
    • 您好,感谢您提供的精彩指南,如果我想更改标题和摘要的值并在布局上实现点击侦听器,我会担心它是如何完成的?谢谢!
    • 为了使布局与其他内置首选项相匹配,您可以在此处找到最新的 androidx 首选项布局:android.googlesource.com/platform/frameworks/support/+/… 复制它,修改它并在更新 AndroidX 时观察变化.
    【解决方案2】:

    我就是这样做的,使用支持库preference-v7

    layout/preference_theme.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button android:id="@+id/theme_light" ... />
        <Button android:id="@+id/theme_dark"... />
        <Button android:id="@+id/theme_sepia"... />
        <Button android:id="@+id/theme_green"... />
    </LinearLayout>
    

    PreferenceTheme.java(自定义 Preference 类)

    import android.support.v7.preference.Preference;
    import android.support.v7.preference.PreferenceViewHolder;
    
    public class PreferenceTheme extends Preference {
    
        public PreferenceTheme(Context context, AttributeSet attrs) {
            this(context, attrs, 0);
        }
    
        public PreferenceTheme(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            setWidgetLayoutResource(R.layout.preference_theme);
        }
    
        @Override
        public void onBindViewHolder(PreferenceViewHolder holder) {
            super.onBindViewHolder(holder);
            holder.itemView.setClickable(false); // disable parent click
            View button = holder.findViewById(R.id.theme_dark);
            button.setClickable(true); // enable custom view click
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // persist your value here
                }
            });
            // the rest of the click binding
        }
    }
    

    preferences.xml

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <android.support.v7.preference.PreferenceCategory
            android:title="Reading">
            <example.com.preference.PreferenceTheme
                android:key="pref_theme"
                android:title="Theme"
                android:defaultValue="light" />
            ...
        </android.support.v7.preference.PreferenceCategory>
    </android.support.v7.preference.PreferenceScreen>
    

    【讨论】:

    • 你能告诉我如何在PreferenceActivity 中使用构造函数参数(上下文和属性)和findPreference 创建和使用PreferenceTheme 实例吗?
    • 我写了一篇关于使用preference-v7的博文,如果你有兴趣hidroh.com/2015/11/30/building-custom-preferences-v7
    • 如果我使用 androidx 而不是支持库,我该如何应用它?
    【解决方案3】:

    通过定义视图和操作,创建自定义首选项类似于创建片段或其他 UI 组件。

    Android 开发者有一个很好的创建设置指南,其中包括一个用于创建自定义首选项的部分: http://developer.android.com/guide/topics/ui/settings.html#Custom

    【讨论】:

    • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接答案可能会失效。 - From Review
    • 考虑到这个答案来自 2013 年,并且 android 对其 API 发布了数十次更新,这实际上可能是一件好事 :) 但我明白你在说什么。
    【解决方案4】:

    您可以根据偏好创建自定义布局,并且可以在 res/xml 中的 Preference 中的 android:layout 属性中设置它,如下所示:

    <Preference
        ......................
        android:layout="@layout/your_layout" />
    

    或者您可以使用 Activity 而不是偏好

    【讨论】:

    • 如果我们试图创建一个自定义的edittextpreference,那么使用android android:layout="@layout/your_layout" 不会在对话框中显示它,而是在主屏幕本身上显示。
    • 我不明白的是,我如何喜欢 your_layout 中的例如微调器?
    • 我想我们永远不会知道
    • 如果我想更新 your_layout 元素我该怎么做
    【解决方案5】:

    或者更好的选择是扩展 DialogPreference 类。您可以将颜色选择器小部件设置为对话框视图,并在扩展首选项本身内获得积极的结果。 Here 是一个类似的问题,但它对您的场景非常有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-05
      • 2011-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多