Android PreferenceCategory标记的摘要字段

Android PreferenceCategory标记的摘要字段,android,android-preferences,android-sharedpreferences,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? -->

我见过这样的情况:

<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>

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

android:summary=“与蔬菜相关的首选项”

当我使用pref屏幕时,摘要显示在视图中,但pref类别不是这种情况。pref类别中摘要的存在是否仅仅是一种可以从某种程度上看到的惯例


pref category元素中summary的实际用法是什么?

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

这是因为默认布局()仅包含标题字段的单个文本视图:

<!-- 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"
/>
其中layout/preference\u category\u 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>


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

您可以使用首选项和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>

@ehartwell绝对正确
我的不同版本的布局是:

用于棒棒糖前的

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="6dp"
    android:textSize="14sp"
    android:textStyle="bold"
    android:textAllCaps="true"
    android:paddingLeft="8dp"
    android:paddingRight="8dp"/>

  <TextView android:id="@android:id/summary"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="-5dp"
    android:textSize="12sp"
    style="?android:attr/listSeparatorTextViewStyle"/>

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="@dimen/preference_category_margin"
    android:paddingRight="@dimen/preference_category_margin"
    android:orientation="vertical">

  <TextView android:id="@android:id/title"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="6dp"
    android:textStyle="bold"
    android:textSize="13sp"
    android:textAllCaps="false"
    android:textColor="@color/colorAccent"/>

  <TextView android:id="@android:id/summary"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="12sp"
    android:textColor="@color/colorAccent"
    android:textAllCaps="false"/>

</LinearLayout>

用于棒棒糖后的

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="6dp"
    android:textSize="14sp"
    android:textStyle="bold"
    android:textAllCaps="true"
    android:paddingLeft="8dp"
    android:paddingRight="8dp"/>

  <TextView android:id="@android:id/summary"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="-5dp"
    android:textSize="12sp"
    style="?android:attr/listSeparatorTextViewStyle"/>

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="@dimen/preference_category_margin"
    android:paddingRight="@dimen/preference_category_margin"
    android:orientation="vertical">

  <TextView android:id="@android:id/title"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="6dp"
    android:textStyle="bold"
    android:textSize="13sp"
    android:textAllCaps="false"
    android:textColor="@color/colorAccent"/>

  <TextView android:id="@android:id/summary"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="12sp"
    android:textColor="@color/colorAccent"
    android:textAllCaps="false"/>

</LinearLayout>

@尺寸/偏好\类别\特殊屏幕尺寸的边距不同
16dp或24dp

它们非常好:)

我发现这非常有效(与更改布局相比;因为它也需要与Android版本的外观相匹配)。此外,如果添加
android:selective=false
,您可以获得类似的外观。