首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >安卓DarkMode :价值之夜不起作用

安卓DarkMode :价值之夜不起作用
EN

Stack Overflow用户
提问于 2020-02-10 09:46:08
回答 2查看 10.5K关注 0票数 8

我正在我的应用程序中开发日夜功能,所以我阅读那些文档并开始开发它。

它很好地工作,在白天或夜间默认值与愉快的方法AppCompatDelegate.setDefaultNightMode(*)

对于自定义主题颜色,我创建值-夜间文件夹,并在内部创建colors.xml文件,如下所示。

代码语言:javascript
复制
res -> values -> colors.xml
res -> values-night -> colors.xml

在我放置那个颜色,但不适用于夜间主题!很奇怪,为什么值夜色并不总是适用于它的默认夜色呢?我研究过一些,但找不到解决办法。

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

这是我的build.gradle文件

提前谢了。

EN

回答 2

Stack Overflow用户

发布于 2020-02-10 11:08:40

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

Step - 1

首先,将文件夹创建到您的资源文件中,如下所示(即值-夜晚)

Step - 2

创建样式、字符串和颜色 xml文件,用于夜间模式,与下面的图像相同,并在应用夜间模式时添加您希望在应用程序中显示的夜间模式颜色、字符串和样式。

要获得更好的用户体验,请在您的样式中添加窗口动画

值-> style.xml

代码语言:javascript
复制
<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>

值-夜晚-> style.xml

代码语言:javascript
复制
<!-- 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

代码语言:javascript
复制
<?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

代码语言:javascript
复制
<?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>

Step - 3

如果希望在安装应用程序时按照设备模式第一次设置夜间模式,则在中添加下面的代码。

代码语言:javascript
复制
@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
}

Step - 4

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

代码语言:javascript
复制
@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
}

Step - 5

使用以下代码更改模式

代码语言:javascript
复制
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.

代码语言:javascript
复制
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;
}

我希望这能帮到你!

谢谢。

票数 6
EN

Stack Overflow用户

发布于 2020-02-10 12:17:40

将appcompat依赖关系从 1.1.0 更改为1.0.0,在1.1.0中有一些需要更新资源的问题。这个把戏对我有用。

代码语言:javascript
复制
 implementation 'androidx.appcompat:appcompat:1.1.0'//Remove 
 implementation 'androidx.appcompat:appcompat:1.0.0' // Add

希望这能帮到你!!

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60147668

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档