Android:创建自定义首选项

Jon*_*nas 47 android preferenceactivity android-preferences android-layout

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

我想像这样编码颜色设置:

颜色偏好

我知道选择颜色很容易实现ListPreference,但是那种"复选框"会很棒.

Dav*_*ave 86

Android Developer页面仅显示如何制作DialogFragment.尽管如此,仍然可以自定义项目的外观Preference.在XML中,您必须将根元素声明为android:id="@android:id/widget_frame,然后声明TextViewandroid: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>
Run Code Online (Sandbox Code Playgroud)

然后,在派生自的类中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);
}
Run Code Online (Sandbox Code Playgroud)

然后在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>
Run Code Online (Sandbox Code Playgroud)

这给出了如下偏好:

Seekbar首选项

您可以轻松地调整此方法以在原始问题中显示行上的多个复选框.


hid*_*dro 37

这就是我使用支持库的方式preference-v7.

偏爱

布局/ 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>
Run Code Online (Sandbox Code Playgroud)

PreferenceTheme.java(自定义首选项)

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
    }
}
Run Code Online (Sandbox Code Playgroud)

的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>
Run Code Online (Sandbox Code Playgroud)

  • 如果你有兴趣,我写了一篇关于在这里使用`preference-v7`的博客文章http://www.hidroh.com/2015/11/30/building-custom-preferences-v7/ (3认同)

tbk*_*n23 8

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

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


sti*_*ike 5

您可以为首选项创建自定义布局,您可以android:layout在res/xml中的Preference中的属性中设置它,如下所示:

<Preference
    ......................
    android:layout="@layout/your_layout" />
Run Code Online (Sandbox Code Playgroud)

或者您可以使用Activity而不是首选项