如何自定义CheckBoxPreference的布局(maxLines属性)?
我目前在使用SettingsFragment(继承自android.support.v7.preference.PreferenceFragmentCompat),通过settings.xml配置偏好项:
<android.support.v7.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <android.support.v7.preference.PreferenceCategory android:title="@string/settings_category_title_general"> <android.support.v7.preference.CheckBoxPreference android:defaultValue="@string/settings_default_value_lorem" android:key="@string/settings_key_lorem" android:summary="@string/settings_summary_lorem" android:title="@string/settings_title_lorem" /> </android.support.v7.preference.PreferenceCategory> </android.support.v7.preference.PreferenceScreen>
发现summary文本在4行后被截断,查源码发现这个限制来自com.android.support:preference-v7:24.2.1的res/layout/preferences.xml:
<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/textAppearanceSmall" android:textColor="?android:attr/textColorSecondary" android:maxLines="4" />
通过布局检查器也确认了summary的ID:

我尝试通过自定义样式覆盖这个设置,定义了以下主题样式:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <item name="preferenceTheme">@style/AppPreferenceTheme</item> </style> <style name="AppPreferenceTheme" parent="@style/PreferenceThemeOverlay"> <item name="preferenceStyle">@style/AppPreference</item> </style> <style name="AppPreference" parent="@style/Preference"> <item name="android:layout">@layout/settings_item</item> </style>
同时复制原preferences.xml为settings_item.xml,修改了其中的android:maxLines数值,但样式并未生效——我给summary视图加了背景色验证过这一点。
问题原因与修复方案
你遇到的问题大概率是样式层级没覆盖到具体的Preference子类(比如你用的是CheckBoxPreference,但只覆盖了基础的PreferenceStyle),或者自定义布局的引用逻辑不对。下面是具体的修复步骤:
确保自定义布局ID完全匹配
复制后的settings_item.xml里,summary对应的TextView必须保留原ID@android:id/summary,这样系统才能正确关联Preference的summary文本。同时修改maxLines为你需要的数值,比如:<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/textAppearanceSmall" android:textColor="?android:attr/textColorSecondary" android:maxLines="10" /> <!-- 修改为你需要的行数,比如10或者设置为integer.MAX_VALUE实现无限制 -->针对具体Preference子类覆盖样式
因为你使用的是CheckBoxPreference,需要在主题中单独覆盖它的样式属性:<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <item name="preferenceTheme">@style/AppPreferenceTheme</item> </style> <style name="AppPreferenceTheme" parent="@style/PreferenceThemeOverlay"> <!-- 覆盖基础Preference样式 --> <item name="preferenceStyle">@style/AppPreference</item> <!-- 覆盖CheckBoxPreference的样式 --> <item name="checkBoxPreferenceStyle">@style/AppCheckBoxPreference</item> </style> <style name="AppPreference" parent="@style/Preference"> <item name="android:layout">@layout/settings_item</item> </style> <style name="AppCheckBoxPreference" parent="@style/CheckBoxPreference"> <item name="android:layout">@layout/settings_item</item> </style>更简便的全局覆盖方式
如果你不想为每个Preference子类单独设置样式,也可以直接在自定义Preference主题中指定全局布局:<style name="AppPreferenceTheme" parent="@style/PreferenceThemeOverlay"> <item name="preferenceLayout">@layout/settings_item</item> </style>这样所有继承自Preference的控件都会自动使用你自定义的布局,无需单独配置子类样式。
验证生效
重新编译运行后,你会看到summary文本不再被限制为4行,背景色的修改也会显示出来,说明自定义布局已经生效。
内容的提问来源于stack exchange,提问作者JJD

