如何使 PreferenceScreen 的标题可调整大小?

问题描述

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
                  android:title="@string/game_title">

我的应用有一个偏好屏幕,我已经为其添加标题。当应用程序在分屏中打开并在窗口中调整为不同大小时,当存在的宽度较小时,标题消失。

尝试添加

android:layout_height="wrap_content"
android:layout_width="wrap_content"

PreferenceScreen属性,但它没有帮助。一些帮助将不胜感激。

解决方法

就像在 this answer 中一样,您可以为 PreferenceScreen 创建自定义布局并将其应用到 android:layout 属性

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
                  android:layout="@layout/preference_title"
                  android:title="@string/game_title">

preference_title.xml 布局

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@android:id/title"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="@dimen/preference_title"/>

并创建不同版本的 res/values/dimen.xml 文件,以具有与可能的屏幕宽度成比例的标题文本大小 (@dimen/preference_title) 的可变值。通常您会有不同的版本 {{1} } 目录,例如对于 w600dp 的屏幕宽度,您将拥有 values 目录。