2

我试图向我的应用程序添加暗模式。当我在我的设置片段上激活暗模式时,屏幕会闪烁,并且页面保持亮模式。但是,当我转换到不同的片段时,无论是通过后台堆栈还是打开新片段,该片段都处于暗模式。只有在返回设置片段时,它才会处于暗模式。但是当我在设置页面上禁用暗模式时,它会立即回到亮模式。为什么它不从浅色模式过渡到深色模式,但它会从深色模式过渡到浅色模式。根据我之前的研究,我将不得不将片段重新加载到活动中,我曾尝试这样做,但没有成功。

在此处输入图像描述

用于设置片段的 XML 布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SettingsFragment"
    android:background="?attr/backgroundColor">

样式.xml:

<resources>
    <!-- Light/Day Mode theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">#424242</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="backgroundColor">#fcfcfc</item>
        <item name="cardBackgroundColor">#ffffff</item>
        <item name="android:textColor">#808080</item>
        <item name="android:textColorSecondary">@android:color/white</item>
        <item name="boxBackgroundColor">#fcfcfc</item>

        <item name="android:windowContentTransitions">true</item>
    </style>

    <!-- Night / dark Mode theme -->
    <style name="darktheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">#212121</item>
        <item name="colorPrimaryDark">#000000</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="backgroundColor">#000000</item>
        <item name="cardBackgroundColor">#424242</item>
        <item name="android:textColor">#ffffff</item>
        <item name="editTextColor">#ffffff</item>
        <item name="android:textColorSecondary">@android:color/white</item>
        <item name="alertDialogTheme">@style/AlertDialogCustom</item>
        <item name="boxBackgroundColor">#303030</item>
        <!-- <item name="backgroundColor">#303030</item> -->

        <item name="android:windowContentTransitions">true</item>
    </style>

设置片段java代码:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.fragment_settings, container, false);

        if(AppCompatDelegate.getDefaultNightMode()==AppCompatDelegate.MODE_NIGHT_YES)
        {
            getActivity().setTheme(R.style.darktheme);
        } else {
            getActivity().setTheme(R.style.AppTheme);
        }
        myswitch = view.findViewById(R.id.myswitch);
        if (AppCompatDelegate.getDefaultNightMode()==AppCompatDelegate.MODE_NIGHT_YES)
        {
            myswitch.setChecked(true);
        }

        myswitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
        {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
            {
                if (isChecked)
                {
                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
                    //Objects.requireNonNull(getActivity()).recreate();
                    //getFragmentManager().beginTransaction().replace(R.id.fragmentContainer, new SettingsFragment()).commit(); //<== both these two lines not working

                } else {
                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);

                }
            }
        });
4

1 回答 1

0

可能是因为在这一行中<style name="darktheme" parent="Theme.AppCompat.Light.NoActionBar">您将父级添加为 Light 而不是 DayNight 所以将此行更改为<style name="darktheme" parent="Theme.AppCompat.DayNight.NoActionBar">,我希望它会起作用

于 2021-05-05T06:56:36.643 回答