gpt4 book ai didi

android - 如何自定义 CheckBoxPreference 的布局 (maxLines)?

转载 作者:行者123 更新时间:2023-11-29 23:56:05 25 4
gpt4 key购买 nike

我正在使用以下 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>

com.android.support:preference-v7:24.2.1 中定义的 四行 后,summary 文本被截断在 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:

Preference layout

为了覆盖设置,我定义了以下样式:

<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 View 中添加了一个 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>

关于android - 如何自定义 CheckBoxPreference 的布局 (maxLines)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50339504/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com