6

我见过这样的事情:

<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="Preferences related to vegetable") ?

当我使用 pref screen 时,视图中会显示摘要,但这不是 pref 类别的情况。pref 类别中摘要的存在是否只是一种可以以某种方式看到的约定?

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

4

3 回答 3

11

标准的 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"
/>

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

<PreferenceCategory android:title="Category" android:summary="This is the summary"
                    android:layout="@layout/preference_category_summary">

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

但是,在您继续执行此操作之前,您应该考虑它对用户来说是更少还是更多的困惑。除非您仔细设置摘要文本的样式,否则它会跳出屏幕或似乎附加到类别中的第一个首选项。

于 2012-04-11T02:27:08.090 回答
8

你可以只使用 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>
于 2013-10-03T18:03:10.743 回答
0

@ehartwell 绝对正确
,我对 diff 版本的布局是:

for pre-lollipop

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

@dimen/preference_category_margin 是特殊屏幕尺寸
16dp 或 24dp的差异
,它们非常好:)

于 2016-05-30T08:44:20.303 回答