【发布时间】:2020-07-19 22:25:11
【问题描述】:
如何更改由 PreferenceFragmentCompat 膨胀的 PreferenceScreen 中的文本和背景颜色?
尝试过How to change the text color of PreferenceCategory/PreferenceScreen,但该解决方案适用于 Preference Activity 而不是 PreferenceFragmentCompat。
还尝试按照How to change text color of preference category in 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>
【问题讨论】:
标签: android kotlin android-fragments preferencescreen