Android:创建自定义首选项 [英] Android: Creating custom preference

查看:32
本文介绍了Android:创建自定义首选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在 PreferenceScreen 中创建个人首选项?

Is it possible to create an individual preference in an PreferenceScreen?

我想对颜色设置进行编码:

I would like to code color settings like that:

我知道使用 ListPreference 选择颜色很容易实现,但是如果使用这种复选框"会很棒.

I know that choosing the color is easy realizable with the ListPreference, but it would be awesome with that kind of "checkboxes".

推荐答案

Android Developer page 只展示了如何制作 DialogFragment.不过,仍然可以自定义 Preference 项目的外观.在您的 XML 中,您必须将根元素声明为 android:id="@android:id/widget_frame,然后将 TextView 声明为 android:titleandroid:summary.然后,您可以声明要出现在布局中的其他元素.这是一个显示 SeekBar 的示例,您可以轻松地适应多复选框颜色选择器.

The Android Developer page only shows how to make a DialogFragment. It's still possible to customise the appearance of a Preference item though. In your XML you have to declare the root element as android:id="@android:id/widget_frame, and then declare TextView as android:title and android:summary. You can then declare other elements you want to appear in the layout. Here's an example showing a SeekBar which you could easily adapt to a multi-checkbox colour chooser.

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() 方法:

Then, in a class that derives from Preference, override the onCreateView() method:

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);
}

然后在首选项.xml 文件中使用首选项:

Then in the preferences.xml file use the preference:

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>

这给出了如下所示的首选项:

This gives a preference that looks as follows:

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

You can easily adapt this method to show multiple checkboxes on a row as was in the original question.

这篇关于Android:创建自定义首选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆