4

我使用以下代码在我的应用程序中切换日/夜模式:

SharedPreferences.Editor editor = sharedPrefs.edit();

AppCompatDelegate.setDefaultNightMode(isChecked
     ? AppCompatDelegate.MODE_NIGHT_YES
     : AppCompatDelegate.MODE_NIGHT_NO);

     if (isChecked) {
        editor.putBoolean("isDarkModeEnabled", true);
        editor.apply();
     } else {
        editor.putBoolean("isDarkModeEnabled", false);
        editor.apply();
}

当我切换模式时,活动会闪烁并重新创建。如何在切换模式时实现更平滑的过渡(例如切换之间的淡入/淡出)?我可以使用 overridePendingTransition(),还是有其他方便的方法?

4

1 回答 1

-4

在您的样式中插入以下代码

<style name="WindowAnimationTransition">
    <item name="android:windowEnterAnimation">@android:anim/fade_in</item>
    <item name="android:windowExitAnimation">@android:anim/fade_out</item>
</style>

并将这一行添加为您的主题。@style/WindowAnimationTransition

然后你有更平滑的过渡。

或访问https://medium.com/@rohitksingh/start-your-activity-with-slide-animation-ae63017e4b7d了解具体活动

于 2020-01-07T18:55:15.257 回答