PreferenceCategory标记的摘要字段

Luk*_*kap 6 android android-preferences android-sharedpreferences

我见过这样的事情:

<PreferenceCategory xmlns:android="http://schemas.android.com/apk/res/android"
    android:key="vegi_category" android:title="Vegetables"

    android:summary="Preferences related to vegetable">  <!-- Why is this here? -->

    <CheckBoxPreference android:key="tomato_selection_pref"
        android:title="Tomato " android:summary="It's actually a fruit" />
    <CheckBoxPreference android:key="potato_selection_pref"
        android:title="Potato" android:summary="My favorite vegetable" />
</PreferenceCategory>
Run Code Online (Sandbox Code Playgroud)

但我不明白为什么pref类别有一个汇总字段:

(android:summary="Preferences related to vegetable")?

当我使用pref屏幕时,摘要会显示在视图中,但这不是pref类别的情况.在pref类别中存在摘要只是它的一个约定可以用某种方式看出来吗?

pref类别元素中摘要的实际用法是什么?

eha*_*ell 10

标准的PreferenceCategory小部件仅显示标题; 该android:summary属性被忽略.

那是因为默认布局(preference_category.xml)只包含标题字段的单个TextView:

<!-- Layout used for PreferenceCategory in a PreferenceActivity. -->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    style="?android:attr/listSeparatorTextViewStyle"
    android:id="@+android:id/title"
/>
Run Code Online (Sandbox Code Playgroud)

如果您还想显示摘要,可以使用该android:layout属性指定自己的布局.例如:

<PreferenceCategory android:title="Category" android:summary="This is the summary"
                    android:layout="@layout/preference_category_summary">
Run Code Online (Sandbox Code Playgroud)

layout/preference_category_summary.xml的位置如下:

<!-- Layout used for PreferenceCategory + SUMMARY in a PreferenceActivity. -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent" android:layout_height="wrap_content"
              android:orientation="vertical">
    <TextView android:id="@+android:id/title" 
              style="?android:attr/listSeparatorTextViewStyle"/>
    <TextView android:id="@+android:id/summary"
              android:paddingLeft="5dip" android:paddingRight="dip"
              android:layout_width="match_parent" android:layout_height="wrap_content"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

然而,在你继续这样做之前,你应该考虑它对用户来说是否更少或更混乱.除非您仔细设置摘要文本的样式,否则它将跳出屏幕或显示为附加到类别中的第一个首选项.


小智 7

你可以使用Preference和android:summary.

<PreferenceCategory xmlns:android="http://schemas.android.com/apk/res/android"
    android:key="vegi_category" android:title="Vegetables">

    <Preference android:summary="Preferences related to vegetable" />

    <CheckBoxPreference android:key="tomato_selection_pref"
        android:title="Tomato " android:summary="It's actually a fruit" />
    <CheckBoxPreference android:key="potato_selection_pref"
        android:title="Potato" android:summary="My favorite vegetable" />
</PreferenceCategory>
Run Code Online (Sandbox Code Playgroud)