英文:
SwitchPreferenceCompat wrong "off" color on real device
问题
I'm using SwitchPreferenceCompat
from androidx.preference:preference:1.2.0
. In Android Studio preview everything looks good:
But in real device it look this:
Here my preferences.xml
:
<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory app:title="@string/auth_header">
<SwitchPreferenceCompat
app:key="@string/auth_key"
app:title="@string/auth_title"
android:icon="@drawable/ic_fingerprint"/>
</PreferenceCategory>
</PreferenceScreen>
And app theme:
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.Notes" parent="Theme.Material3.DayNight.NoActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/color_primary</item>
<item name="colorPrimaryVariant">@color/color_primary_variant</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/color_secondary</item>
<item name="colorSecondaryVariant">@color/color_secondary_variant</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor">@android:color/transparent</item>
<!-- Customize your theme here. -->
<item name="android:windowLightStatusBar">true</item>
<item name="windowActionModeOverlay">true</item>
<item name="colorAccent">@color/black</item>
</style>
</resources>
Note that colorAccent
is not related to this problem. Without it the result is the same (even if you remove all values from the theme, the result will not change). I use it just to set the preference header color.
Question: Is this a library bug or am I doing something wrong?
Device: POCO X3 NFC (MIUI 12.5, Android 11).
androidx.preference:preference:1.2.0
com.google.android.material:material:1.8.0
英文:
I'm using SwitchPreferenceCompat
from androidx.preference:preference:1.2.0
. In Android Studio preview everything looks good:
But in real device it look this:
Here my preferences.xml
:
<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory app:title="@string/auth_header">
<SwitchPreferenceCompat
app:key="@string/auth_key"
app:title="@string/auth_title"
android:icon="@drawable/ic_fingerprint"/>
</PreferenceCategory>
</PreferenceScreen>
And app theme:
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.Notes" parent="Theme.Material3.DayNight.NoActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/color_primary</item>
<item name="colorPrimaryVariant">@color/color_primary_variant</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/color_secondary</item>
<item name="colorSecondaryVariant">@color/color_secondary_variant</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor">@android:color/transparent</item>
<!-- Customize your theme here. -->
<item name="android:windowLightStatusBar">true</item>
<item name="windowActionModeOverlay">true</item>
<item name="colorAccent">@color/black</item>
</style>
</resources>
Note that colorAccent
is not related to this problem. Without it the result is the same (even if you remove all values from the theme, the result will not change). I use it just to set the preference header color.
Question: Is this a library bug or am I doing something wrong?
Device: POCO X3 NFC (MIUI 12.5, Android 11).
androidx.preference:preference:1.2.0
com.google.android.material:material:1.8.0
答案1
得分: 0
这可能是与SwitchCompat
小部件与Material3
主题相关的错误。
我在以下位置报告了问题:
https://issuetracker.google.com/issues/271871302
https://github.com/material-components/material-components-android/issues/3295
我最终使用了自己的设置实现(常规片段+带有MaterialSwitch
的布局),因为实现自己的解决方案比处理这些错误更容易和更快。
这还增加了更多的自定义选项,但现在我必须手动更新设置(我使用常规的ViewModel
来保持状态和自己的PreferencesRepository
来保存/加载首选项)。
英文:
This is probably a bug with the SwitchCompat
widget related to the Material3
theme.
I opened issues here:
https://issuetracker.google.com/issues/271871302
https://github.com/material-components/material-components-android/issues/3295
I ended up with own settings implementation (regular fragment + layout with MaterialSwitch
) because it's easier and faster implement own solution than dealing with these bugs.
It also adds more customization, but now I have to update settings manually (I use a regular ViewModel
to keep the state and my own PreferencesRepository
to save/load preferences).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论