android - Android PreferenceFragmentCompat、首选项标题和摘要未显示
问题描述
我正在使用androidx.preference:preference:1.1.1
只是想在我现有的应用程序中集成一个简单的设置页面。
首选项title
,summary
不会出现。
我什至将背景从白色更改为黑色。
甚至还放了一个长文本来看看大小是否改变(它没有改变)
我把截图放在下面
还使用DrawerLayout
和 Jetpack Navigation ( androidx.navigation:navigation-ui:2.3.0
)
SettingsFragment.kt
class SettingsFragment : PreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.preferences, rootKey)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
listView.setBackgroundColor(Color.rgb(26, 26, 26))
}
}
preferences.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="https://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="Preference Category">
<CheckBoxPreference
android:key="checkbox"
android:summary="Tap to check if on or off"
android:title="Checkbox Preference" />
<SwitchPreference
android:key="switch"
android:title="Switch Preference"
android:summary="Click to switch on or off"
android:defaultValue="true"/>
</PreferenceCategory>
</PreferenceScreen>
styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.DayNight.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:textColor">@color/colorWhite</item>
<item name="android:textColorHint">@color/colorWhite</item>
<item name="alertDialogTheme">@style/AlertDialogTheme</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
<style name="AlertDialogTheme" parent="ThemeOverlay.AppCompat.Dialog.Alert">
<item name="android:background">@color/colorPrimaryDark</item>
<item name="buttonBarNegativeButtonStyle">@style/NegativeButtonStyle</item>
<item name="buttonBarPositiveButtonStyle">@style/PositiveButtonStyle</item>
</style>
<style name="NegativeButtonStyle" parent="Widget.AppCompat.Button.ButtonBar.AlertDialog">
<item name="android:background">@color/colorPrimaryDark</item>
<item name="android:textColor">@color/colorGrayLight</item>
</style>
<style name="PositiveButtonStyle" parent="Widget.AppCompat.Button.ButtonBar.AlertDialog">
<item name="android:background">@color/colorPrimaryDark</item>
<item name="android:textColor">@color/colorYellowDarker</item>
</style>
</resources>
解决方案
Android Studio 布局应在“设计”选项卡下的预览中显示字符串。也可能是您覆盖了样式 xml 中的某些内容。
您还可以创建一个新的 Android Studio 项目,并仅进行设置以检查您的项目中是否有问题。
我的解决方案是一种解决方法,因为我在颜色方面遇到了样式问题。
您为首选项视图创建自定义布局。我称这个布局
偏好复选框自定义
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:gravity="center_vertical"
android:paddingStart="45dp"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:background="?android:attr/selectableItemBackground"
android:clipToPadding="false"
android:baselineAligned="false">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:paddingTop="16dp"
android:paddingBottom="16dp">
<TextView
android:id="@android:id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:textColor="#FF0000"
android:textSize="16sp"
android:ellipsize="marquee"/>
<TextView
android:id="@android:id/summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:textAlignment="viewStart"
android:textColor="#FF0000"
android:maxLines="10" />
</LinearLayout>
<!-- Preference should place its actual preference widget here. -->
<CheckBox
android:id="@android:id/checkbox"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="end|center_vertical"
android:paddingStart="16dp"
android:paddingEnd="0dp"
android:focusable="false"
android:clickable="false"
android:buttonTint="@color/colorAccent" />
</LinearLayout>
然后在首选项 xml 中设置自定义布局。
<CheckBoxPreference
android:key="keySomething"
android:title="Tap to check if on or off"
android:layout="@layout/preference_checkbox_custom"/>
我将文本颜色设置为#FF0000。如果它仍然不起作用,那么它的其他东西
推荐阅读
- android - Android Studio 4.1 中的 Gradel 构建问题
- nginx - 从 NGINX-RTMP 实时模式获取时的音视频延迟
- azure-powershell - 如何更新自动化帐户中的模块文件?
- c++ - 如何在编译时检查所有编译器的 RTTI?
- sql - 使用最近的(最旧的日期)使用同一列计算差异
- typescript - 针对 TypeScript 中的接口验证对象的模式
- r - 将新列分配给 R 中第二个数据框的数据框列表
- ios - swift问题数组无法打印到collectionviewcell标签
- plotly - 单击 Plotly.js 时保持悬停框
- redis - 是什么导致 Azure Redis 缓存中的 RedisTimeoutException?