如何自定义 CheckBoxPreference 的布局 (maxLines)?
How to customize the layout (maxLines) of a CheckBoxPreference?
我正在使用以下 settings.xml
来膨胀我的 SettingsFragment
扩展 android.support.v7.preference.PreferenceFragmentCompat
:
<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
文本在四行后被截断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>
并创建了一个名为 settings_item.xml
的原始布局文件 preferences.xml
的副本,它覆盖了 android:maxLines="4"
.
但是,样式未应用。我在 summary
视图中添加了 background
颜色来检查这一点。
相关
来自JavaDoc of the Preference
constructor:
Perform inflation from XML and apply a class-specific base style. This
constructor of Preference allows subclasses to use their own base style
when they are inflating. For example, a CheckBoxPreference
constructor calls this version of the super class constructor and
supplies android.R.attr.checkBoxPreferenceStyle
for
defStyleAttr
. This allows the theme's checkbox preference
style to modify all of the base preference attributes as well as the
CheckBoxPreference
class's attributes.
所以将样式更改为:
<style name="AppPreferenceTheme" parent="@style/PreferenceThemeOverlay">
<item name="checkBoxPreferenceStyle">@style/AppPreference</item>
</style>
<style name="AppPreference" parent="@style/Preference.CheckBoxPreference">
<item name="layout">@layout/settings_item</item>
</style>
我正在使用以下 settings.xml
来膨胀我的 SettingsFragment
扩展 android.support.v7.preference.PreferenceFragmentCompat
:
<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
文本在四行后被截断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>
并创建了一个名为 settings_item.xml
的原始布局文件 preferences.xml
的副本,它覆盖了 android:maxLines="4"
.
但是,样式未应用。我在 summary
视图中添加了 background
颜色来检查这一点。
相关
来自JavaDoc of the Preference
constructor:
Perform inflation from XML and apply a class-specific base style. This constructor of Preference allows subclasses to use their own base style when they are inflating. For example, a
CheckBoxPreference
constructor calls this version of the super class constructor and suppliesandroid.R.attr.checkBoxPreferenceStyle
fordefStyleAttr
. This allows the theme's checkbox preference style to modify all of the base preference attributes as well as theCheckBoxPreference
class's attributes.
所以将样式更改为:
<style name="AppPreferenceTheme" parent="@style/PreferenceThemeOverlay">
<item name="checkBoxPreferenceStyle">@style/AppPreference</item>
</style>
<style name="AppPreference" parent="@style/Preference.CheckBoxPreference">
<item name="layout">@layout/settings_item</item>
</style>