创建自定义preference

创建自定义preference

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

问题描述

是否有可能创建一个单独的preference在 preferenceScreen

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

我想$像C $ C颜色设置:

I would like to code color settings like that:

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

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

推荐答案

在Android开发者页面只显示了如何做一个DialogFragment。它仍有可能,虽然定制preference项的外观。在你的XML你必须声明的根元素作为ID:widget_frame,然后声明textviews的标题和摘要。然后,你可以声明要显示在布局中的其他元素。下面是一个例子,显示一个搜索栏,你可以很容易地适应多框颜色选择。

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 id:widget_frame, and then declare textviews as title and 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

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:

搜索栏preference.java

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文件中使用了preference:

Then in the preferences.xml file use the preference:

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看起来如下:

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.

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

08-14 10:38