请不要指向How to wrap preference title?,因为它不适用于(如我所评论)你使用@strings/
引用strings.xml文件的情况。
如果你使用
android:title="@string/some_string"
并把
<string name="some_string">the string I want \n to wrap</string>
在strings.xml中,\n
被忽略。
我认为首选屏幕内的所有title
s都应该是单行。
我不认为宣称android:title="The title of this preference\nis this."
会起作用。这也将忽略\n
。
所以,我的建议是,制作标题单行并做一个摘要来描述它。
<CheckBoxPreference android:key="extention"
android:title="@string/title"
android:summary="@string/summary"
android:defaultValue="true"
/>
注意:\n
将用于摘要
对于大多数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)中复制它,并以类似的方式对其进行自定义。