【发布时间】:2013-04-13 01:58:34
【问题描述】:
是否可以在PreferenceScreen 中创建个人偏好?
我想这样编码颜色设置:
我知道使用ListPreference 可以很容易地选择颜色,但是使用那种“复选框”会很棒。
【问题讨论】:
标签: android android-layout android-preferences preferenceactivity
是否可以在PreferenceScreen 中创建个人偏好?
我想这样编码颜色设置:
我知道使用ListPreference 可以很容易地选择颜色,但是使用那种“复选框”会很棒。
【问题讨论】:
标签: android android-layout android-preferences preferenceactivity
Android 开发者页面仅显示如何创建DialogFragment。不过,仍然可以自定义 Preference 项目的外观。在您的 XML 中,您必须将根元素声明为 android:id="@android:id/widget_frame,然后将 TextView 声明为 android:title 和 android: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>
这给出了如下所示的首选项:
您可以轻松调整此方法以在一行中显示多个复选框,就像在原始问题中一样。
【讨论】:
SeekBarPreference 的布局实际上可以在这里找到:<andorid-sdk>/platforms/android-24/data/res/layout/preference_widget_seekbar.xml。 Android 布局不使用widget_frame id 作为根元素,你为什么说必须这样做?实际上,XML 中有一条注释说“Preference 应该将其实际的首选项小部件放在这里”。 在标题和摘要TextViews 之后,但在LinearLayout 之前,ID 为“widget_frame”。
我就是这样做的,使用支持库preference-v7。
Preference 并覆盖onBindViewHolder()。此方法允许您通过 ViewHolder 对象获取对您偏好视图的引用。setWidgetLayoutResource() 或setLayoutResource() 扩展您的自定义视图。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
通过定义视图和操作,创建自定义首选项类似于创建片段或其他 UI 组件。
Android 开发者有一个很好的创建设置指南,其中包括一个用于创建自定义首选项的部分: http://developer.android.com/guide/topics/ui/settings.html#Custom
【讨论】:
您可以根据偏好创建自定义布局,并且可以在 res/xml 中的 Preference 中的 android:layout 属性中设置它,如下所示:
<Preference
......................
android:layout="@layout/your_layout" />
或者您可以使用 Activity 而不是偏好
【讨论】:
或者更好的选择是扩展 DialogPreference 类。您可以将颜色选择器小部件设置为对话框视图,并在扩展首选项本身内获得积极的结果。 Here 是一个类似的问题,但它对您的场景非常有用。
【讨论】: