【问题标题】:Android DarkMode : Value night not workingAndroid暗模式:价值之夜不起作用
【发布时间】:2020-02-10 09:46:08
【问题描述】:

我正在我的应用程序中开发日/夜功能,因此我阅读了这些文档并开始开发它。

在白天或夜晚的默认值下工作正常 使用精巧的方法AppCompatDelegate.setDefaultNightMode(*)

为了自定义 night 主题颜色,我创建 values-night 文件夹,并在其中创建 colors.xml 文件,如下所示。

res -> values -> colors.xml
res -> values-night -> colors.xml

在我放置该颜色但未应用到夜间主题之后!很奇怪为什么价值夜间颜色并不总是应用其显示的默认夜间颜色? 我研究了一些,但找不到解决方案。

注意:重新创建活动已解决我的问题,但我不想重新创建

这是我的 build.gradle 文件

提前谢谢。

【问题讨论】:

标签: android android-theme android-darkmode


【解决方案1】:

试试下面我正在使用的暗模式代码。

步骤 - 1

首先在您的资源文件中创建 night 文件夹,如下图所示(即 values-night)

步骤 - 2

夜间模式创建样式、字符串和颜色 xml文件,如下图所示,并添加您想要在应用中显示的夜间模式颜色、字符串和样式何时应用夜间模式。

为了更好的用户体验,在您的风格中添加窗口动画

值 --> style.xml

<resources xmlns:tools="http://schemas.android.com/tools">

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="NoActionBarAppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/white</item>
        <item name="colorPrimaryDark">@color/white</item>
        <item name="colorAccent">@color/main_click_txt</item>
        <item name="android:windowLightStatusBar" tools:targetApi="m">true</item>
    </style>

    <!-- Add this theme where you change mode -->
    <style name="NoActionBarWithChangeTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/white</item>
        <item name="colorPrimaryDark">@color/white</item>
        <item name="colorAccent">@color/main_click_txt</item>
        <item name="android:windowAnimationStyle">@style/WindowAnimationTransition</item>
        <item name="android:windowLightStatusBar" tools:targetApi="m">true</item>
    </style>

    <!-- This will set the fade in animation on your change theme activity by default -->
    <style name="WindowAnimationTransition">
        <item name="android:windowEnterAnimation">@anim/fade_in</item>
        <item name="android:windowExitAnimation">@anim/fade_out</item>
    </style>

</resources>

values-night --> style.xml

<!-- Base application theme. values-night.xml -->
<style name="NoActionBarAppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/white</item>
    <item name="colorPrimaryDark">@color/white</item>
    <item name="colorAccent">@color/main_click_txt</item>
    <item name="android:windowLightStatusBar" tools:targetApi="m">true</item>
</style>

<!-- Add this theme where you change mode -->
<style name="NoActionBarWithChangeTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/white</item>
    <item name="colorPrimaryDark">@color/white</item>
    <item name="colorAccent">@color/main_click_txt</item>
    <item name="android:windowAnimationStyle">@style/WindowAnimationTransition</item>
    <item name="android:windowLightStatusBar" tools:targetApi="m">true</item>
</style>

<!-- This will set the fade in animation on your change activity by default -->
<style name="WindowAnimationTransition">
    <item name="android:windowEnterAnimation">@anim/fade_in</item>
    <item name="android:windowExitAnimation">@anim/fade_out</item>
</style>

fade_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/linear_interpolator">
    <alpha
        android:duration="2000"
        android:fromAlpha="0.0"
        android:toAlpha="1.0">
    </alpha>
</set>

fade_out.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator">
    <alpha
        android:duration="2000"
        android:fromAlpha="1.0"
        android:toAlpha="0.0" >
    </alpha>
</set>

步骤 - 3

如果您想在第一次安装应用程序时根据设备模式设置夜间模式,请在您的启动画面中添加以下代码。

@Override
protected void onCreate(Bundle savedInstanceState) {
    if (!CommonUtils.isToogleEnabled(SplashActivity.this)) {
        if (CommonUtils.isDarkMode(SplashActivity.this)) {
            CommonUtils.setIsNightModeEnabled(SplashActivity.this, true);
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
        } else {
            CommonUtils.setIsNightModeEnabled(SplashActivity.this, false);
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
        }
    } else {
        if (CommonUtils.isNightModeEnabled(SplashActivity.this)) {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
        } else {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
        }
    }
    super.onCreate(savedInstanceState);

    ...
    //your code
}

步骤 - 4

在您的所有活动中添加以下代码

@Override
protected void onCreate(Bundle savedInstanceState) {
    if (CommonUtils.isNightModeEnabled(MainActivity.this)) {
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
    } else {
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
    }
    super.onCreate(savedInstanceState);

    ...
    //your code
}

步骤 - 5

改变模式使用下面的代码

private WeakReference<Activity> mActivity;

binding.imgNightMode.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        mActivity = new WeakReference<Activity>(MainActivity.this);
        CommonUtils.setIsToogleEnabled(MainActivity.this, true);
        if (CommonUtils.isNightModeEnabled(MainActivity.this)) {
            CommonUtils.setIsNightModeEnabled(MainActivity.this, false);
            mActivity.get().recreate();
        } else {
            CommonUtils.setIsNightModeEnabled(MainActivity.this, true);
            mActivity.get().recreate();
        }
    }
});

以下方法是 CommonUtils

private static final String NIGHT_MODE = "NIGHT_MODE";
private static final String TOOGLE = "TOOGLE";

public static boolean isNightModeEnabled(Context context) {
    SharedPreferences mPrefs = context.getSharedPreferences("MY_PREF", MODE_PRIVATE);
    return mPrefs.getBoolean(NIGHT_MODE, false);
}

public static void setIsNightModeEnabled(Context context, boolean isNightModeEnabled) {
    SharedPreferences mPrefs = context.getSharedPreferences("MY_PREF", MODE_PRIVATE);
    SharedPreferences.Editor editor = mPrefs.edit();
    editor.putBoolean(NIGHT_MODE, isNightModeEnabled);
    editor.apply();
}

public static void setIsToogleEnabled(Context context, boolean isToogleEnabled) {
    SharedPreferences mPrefs = context.getSharedPreferences("MY_PREF", MODE_PRIVATE);
    SharedPreferences.Editor editor = mPrefs.edit();
    editor.putBoolean(TOOGLE, isToogleEnabled);
    editor.apply();
}

public static boolean isToogleEnabled(Context context) {
    SharedPreferences mPrefs = context.getSharedPreferences("MY_PREF", MODE_PRIVATE);
    return mPrefs.getBoolean(TOOGLE, false);
}

public static boolean isDarkMode(Activity activity) {
    return (activity.getResources().getConfiguration()
            .uiMode & Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES;
}

希望对你有帮助!

谢谢。

【讨论】:

  • 如果我不想重新创建呢?你有什么解决办法吗
  • @MohitSuthar 因此,您需要重新开始相同的活动,因为如果不重新创建,您看不到任何变化
  • 谢谢你的回答,但我不是在找这个
  • 如果不重新创建,您将无法更改它们stackoverflow.com/a/25483738/11158194
  • 这并不能回答为什么在夜间模式激活时不使用values-night\colors.xml 的问题。
【解决方案2】:

将 appcompat 依赖从 1.1.0 更改为 1.0.0,在 1.1.0 中更新资源存在一些问题。这个技巧对我有用。

 implementation 'androidx.appcompat:appcompat:1.1.0'//Remove 
 implementation 'androidx.appcompat:appcompat:1.0.0' // Add

希望对你有帮助!!

【讨论】:

  • 我会尽力让你知道
  • 你用过这样的应用主题吗?
  • 是的,但我使用的是 MaterialComponent.DayNight 主题
  • 救命稻草!这解决了 setDefaultNightMode 导致 res id not found 异常的问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-11
  • 1970-01-01
相关资源
最近更新 更多