【问题标题】:The summary field of PreferenceCategory tagPreferenceCategory 标签的汇总字段
【发布时间】:2011-11-02 09:59:17
【问题描述】:

我见过这样的:

<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的实际用法是什么?

【问题讨论】:

    标签: android android-preferences android-sharedpreferences


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      您可以只使用 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>
      

      【讨论】:

      • 我发现这非常有效(与更改布局相比;因为它也需要匹配 Android 版本的外观和感觉)。此外,如果您添加android:selectable=false,您可以实现类似的外观和感觉。
      【解决方案3】:

      @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
      他们很好:)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-07
        • 1970-01-01
        相关资源
        最近更新 更多