【问题标题】:Preference Screen adding a entity header首选项屏幕添加实体标题
【发布时间】:2022-11-12 17:31:45
【问题描述】:

我怎样才能添加这样的所谓“实体标题" 在此处的文档中列出的 PreferenceScreen 中: https://source.android.com/devices/tech/settings/settings-guidelines#entity_header https://source.android.com/devices/tech/settings/settings-guidelines#user_education

我在 Jetpack Preferences 或其他资源中找不到任何元素,如何在 PreferenceScreen 中添加此类标头。

【问题讨论】:

    标签: android android-preferences


    【解决方案1】:

    没有实体头内置首选项。您必须创建自己的首选项。

    1. 使用您自己的实体标头设计创建一个 xml 文件。这是 res/layout 文件夹中的一个文件。就我而言:entity_preference.xml

       <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:orientation="horizontal"
       android:background="@color/md_theme_light_secondaryContainer"
       android:gravity="center_horizontal|center_vertical"
       android:paddingVertical="16dp"
       xmlns:android="http://schemas.android.com/apk/res/android">
      
       <ImageView
           android:id="@+id/entity_image"
           android:layout_gravity="center_horizontal"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:src="@mipmap/ic_launcher_foreground" />
      
       <LinearLayout
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_marginHorizontal="6dp"
           android:orientation="vertical">
       <TextView
           android:id="@+id/entity_text"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="Title"
           android:textAppearance="@style/TextAppearance.Material3.HeadlineSmall"
           />
      
       <TextView
           android:id="@+id/entity_description"
           android:textAppearance="@style/TextAppearance.Material3.BodySmall"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="Brief description"
           />
       </LinearLayout>
      
    2. 在您的首选项屏幕中使用该自定义设计。在我的例子中,这是 res/xml 中的一个 xml 文件:偏好屏幕.xml

       <PreferenceScreen
       xmlns:app="http://schemas.android.com/apk/res-auto"
       xmlns:android="http://schemas.android.com/apk/res/android">
      
       <Preference
           android:layout="@layout/entity_preference"/>
      
       <PreferenceCategory app:title="General settings">
      
           <EditTextPreference
               app:key="title_text"
               app:title="Title"
               app:useSimpleSummaryProvider="true" />
      
      
       </PreferenceCategory>
      
      [...]
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-16
      • 1970-01-01
      • 2023-03-14
      • 1970-01-01
      • 2013-08-10
      • 1970-01-01
      • 2014-09-11
      • 1970-01-01
      相关资源
      最近更新 更多