繁体   English   中英

如何在 Android 中更改 PreferenceScreen 的文本颜色和背景颜色

[英]How to change text color and background color of PreferenceScreen in Android

如何更改由 PreferenceFragmentCompat 膨胀的PreferenceScreen中的文本和背景颜色?

尝试了如何更改 PreferenceCategory/PreferenceScreen 的文本颜色,但该解决方案适用于 Preference Activity 而不是 PreferenceFragmentCompat。

还尝试按照如何在 Android 中更改首选项类别的文本颜色在首选项屏幕中使用布局标签? 但不保存偏好。

class FragmentSettings: PreferenceFragmentCompat() {

    private lateinit var viewModel: SharedViewModel

    override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
        addPreferencesFromResource(R.xml.root_preferences)

        val application = requireNotNull(this.activity).application
        val dataBase = DataBase.getInstance(application)
        val repo = Repo(dataBase!!)

        viewModel = ViewModelProvider(this,
            SharedViewModelFactory(
                dataBase
            )
        ).get(SharedViewModel::class.java)

        (activity as MainActivity).supportActionBar?.title = "Settings"
}
            

这是我的 settingsPreference.xml 中的代码。

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@color/colorExtra"
    android:backgroundTint="@color/colorExtra">

    <PreferenceCategory android:layout="@layout/pref_title" app:title="@string/location_header">

        <SwitchPreferenceCompat
            android:background="@color/colorExtra"
            android:backgroundTint="@color/colorExtra"
            app:defaultValue="true"
            android:layout= "@layout/switch_pref_item"
            app:disableDependentsState="true"
            app:key="USE_DEVICE_LOCATION"
            app:summary="Allow the app to get your location"
            app:title="@string/your_location_title" />

        <EditTextPreference
            android:background="@color/colorExtra"
            android:backgroundTint="@color/colorExtra"
            app:dependency="USE_DEVICE_LOCATION"
            app:key="setLocation"
            android:layout="@layout/set_location_pref"
            app:title="@string/set_location_title"
            app:useSimpleSummaryProvider="true" />


    </PreferenceCategory>

</PreferenceScreen>

这应该通过 styles 完成,并遵循用于设置任何布局样式的相同程序。 它不是通过首选项 xml 完成的。

制作一个 xml 样式,它是Theme.AppCompat的子样式。 然后,您可以覆盖android:windowBackgroundandroid:textColor的 colors 。 您可以将此样式应用于清单中的活动。

例如,在styles.xml中:

<color name="preferencesTextColor">$ff0000</color>
<color name="preferencesBackgroundColor">#202020</color>

<style name="MyPreferencesTheme" parent="Theme.AppCompat">
    <item name="android:textColor">@color/preferencesTextColor</item>
    <item name="android:windowBackground">@color/preferencesBackgroundColor</item>
</style>

然后在清单中,将主题应用于活动:

<activity android:name="MyPreferencesActivity"
    android:theme="@style/MyPreferencesTheme"
    android:label="@string/myPreferenceActivityLabel" />

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM