SwitchPreferenceCompat 在真实设备上显示错误的 “off” 颜色

huangapple go评论69阅读模式
英文:

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:

SwitchPreferenceCompat 在真实设备上显示错误的 “off” 颜色

But in real device it look this:

SwitchPreferenceCompat 在真实设备上显示错误的 “off” 颜色

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:

SwitchPreferenceCompat 在真实设备上显示错误的 “off” 颜色

But in real device it look this:

SwitchPreferenceCompat 在真实设备上显示错误的 “off” 颜色

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的布局),因为实现自己的解决方案比处理这些错误更容易和更快。

SwitchPreferenceCompat 在真实设备上显示错误的 “off” 颜色

这还增加了更多的自定义选项,但现在我必须手动更新设置(我使用常规的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.

SwitchPreferenceCompat 在真实设备上显示错误的 “off” 颜色

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).

huangapple
  • 本文由 发表于 2023年3月7日 20:23:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75661935.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定