问题描述
我正在使用androidx.preference:preference:1.1.1
只是想在我现有的应用程序中集成一个简单的“设置”页面。
首选项的title
和summary
不会显示。
我什至把背景从白色变成了黑色。
甚至还要放一个长文本,以查看大小是否改变(没有改变)
我已将屏幕截图放在下面
还使用DrawerLayout
和Jetpack导航(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项目,并仅进行设置以检查项目中是否存在问题。
我的解决方案是一种解决方法,因为我在颜色方面存在样式问题。
您为首选项视图创建自定义布局。我叫这种布局
preference_checkbox_custom
<?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。如果仍然无法正常工作,那就别的了