【问题标题】:Preference Screen adding a entity header首选项屏幕添加实体标题
【发布时间】:2022-11-12 17:31:45
【问题描述】:
标签:
android
android-preferences
【解决方案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>
-
在您的首选项屏幕中使用该自定义设计。在我的例子中,这是 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>
[...]