对于大多数 Android 版本,没有公开的 API 允许在 Preference 标题上换行。这导致了中长标题上的文本被截断/褪色的不幸常见错误,尤其是在纵向的小屏幕设备上:
<PreferenceScreen
android:key="my_pref_key"
android:title="A Long Preference Title that should wrap to multiple lines" />
但是有两种方法可以解决它:
1.简单的方法 - 但这仅适用于 Android 8 (API 26) 及更高版本:
像这样使用android:singleLineTitle="false":
<PreferenceScreen
android:key="my_pref_key"
android:title="A Long Preference Title that should wrap to multiple lines"
android:singleLineTitle="false" />
2。艰难的方式 - 自定义偏好子布局 - 这适用于所有 Android 版本:
对于标题较长的首选项,请在其上使用自定义布局 (like this),其中TextView 已明确关闭单行文本。但是您必须以编程方式设置布局,因为 Android 4(Holo 主题)和 Android 5 及更高版本(Material 主题)的主题不同。
SettingsFragment.java 或 SettingsActivity.java:
PreferenceScreen myPreferenceItem = (PreferenceScreen)
getPreferenceScreen().findPreference("my_pref_key");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
myPreferenceItem.
setLayoutResource(R.layout.preference_child_material_customized);
} else {
myPreferenceItem.
setLayoutResource(R.layout.preference_child_holo_customized);
}
res/layout/preference_child_material_customized.xml: 的示例
从您的 Android SDK/platforms/android-22/data/res/layout/ 复制此文件
preference_child_material.xml (like this one) 并对其进行自定义:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="?android:attr/listPreferredItemPaddingStart"
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd">
<LinearLayout
android:id="@+android:id/icon_frame"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:minWidth="40dip"
android:gravity="start|center_vertical"
android:orientation="horizontal">
<ImageView
android:id="@+android:id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dip" />
</LinearLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingTop="16dip"
android:paddingBottom="16dip">
<TextView android:id="@+android:id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="false"
android:textAppearance="?android:attr/textAppearanceListItem" />
<TextView android:id="@+android:id/summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@android:id/title"
android:layout_alignStart="@android:id/title"
android:textAppearance="?android:attr/textAppearanceListItemSecondary"
android:textColor="?android:attr/textColorSecondary"
android:maxLines="10" />
</RelativeLayout>
<!-- Preference should place its actual preference widget here. -->
<LinearLayout android:id="@+android:id/widget_frame"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:minWidth="58dip"
android:gravity="end|center_vertical"
android:orientation="vertical" />
</LinearLayout>
注意 TextView 上的 android:singleLine="false" 行。
对于res/layout/preference_child_holo_customized.xml,你应该从你的Android SDK/platforms/android-19/data/res/layout/复制它
preference_child.xml 或 preference_child_holo.xml (like this one) 并以类似的方式对其进行自定义。