Android:创建自定义首选项

时间:2013-04-19 15:51:56

标签: android android-layout android-preferences preferenceactivity

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

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

Color Preference

我知道选择颜色很容易通过ListPreference实现,但是对于那种"复选框"它会很棒。

5 个答案:

答案 0 :(得分:84)

Android Developer页面仅显示如何制作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>

这给出了如下偏好:

Seekbar preference item

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

答案 1 :(得分:32)

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

preference

<强>布局/ 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 (自定义偏好类)

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>

答案 2 :(得分:8)

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

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

答案 3 :(得分:5)

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

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

或者您可以使用“活动”而非“偏好

答案 4 :(得分:0)

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