安卓:创建自定义的偏好

问题描述 投票:44回答:5

是否有可能建立在PreferenceScreen个人爱好?

我想编写这样的颜色设置:

我知道,选择的颜色是很容易实现的与ListPreference,但它是真棒与那种“复选框”的。

android android-layout android-preferences preferenceactivity
5个回答
86
投票

Android开发者页面只显示如何使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>

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

seek bar preference.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>

这给出了一个首选项,如下所示:

您可以轻松地适应这种方法显示在一排多个复选框就像在原来的问题。


36
投票

这就是我如何做到这一点,利用支持库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>

8
投票

创建自定义的偏好是类似于创建片段或其他UI组件,通过定义视图和操作。

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


5
投票

您可以为喜好创建自定义布局,您可以在资源优先设置在android:layout属性/ XML是这样的:

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

或者你可以使用一个活动,而不是偏好


0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.