在PreferenceScreen
中创建个性化偏好设置是否可能?
我想编写像这样的颜色设置:
我知道使用ListPreference
很容易选择颜色,但有那种“复选框”会更好。
在PreferenceScreen
中创建个性化偏好设置是否可能?
我想编写像这样的颜色设置:
我知道使用ListPreference
很容易选择颜色,但有那种“复选框”会更好。
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>
这会创建一个如下所示的偏好设置:
你可以轻松地将此方法调整为在同一行上显示多个复选框,就像原始问题中那样。
这是我的做法,使用支持库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(自定义首选项类)
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
实例吗? - Sdghasemipreference-v7
зҡ„еҚҡе®ўж–Үз« пјҢжӮЁеҸҜд»ҘеңЁиҝҷйҮҢйҳ…иҜ»пјҡhttp://www.hidroh.com/2015/11/30/building-custom-preferences-v7/гҖӮ - hidro创建自定义的偏好设置与创建片段或其他UI组件类似,需要定义视图和操作。
Android开发者有一个很好的指南,介绍如何创建设置,其中包括创建自定义偏好设置的部分: http://developer.android.com/guide/topics/ui/settings.html#Custom
android:layout
属性。<Preference
......................
android:layout="@layout/your_layout" />
或者您可以使用Activity而不是Preference。
onCreateView()
被调用了两次,你应该调用super.onCreate()
。 - theroom101SeekBarPreference
的布局实际上可以在此处找到:<andorid-sdk>/platforms/android-24/data/res/layout/preference_widget_seekbar.xml
。Android布局不使用widget_frame
ID作为根元素,为什么您要这样做?实际上,在XML中有一个注释说“首选项应将其实际的首选项小部件放置在此处”,该注释位于标题和摘要TextView
之后,但在具有IDwidget_frame'
的LinearLayout
之前。 - user905686