Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/181.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在闪屏上有一个适当的黑暗和光明的主题_Android_Android Layout - Fatal编程技术网

Android 如何在闪屏上有一个适当的黑暗和光明的主题

Android 如何在闪屏上有一个适当的黑暗和光明的主题,android,android-layout,Android,Android Layout,我读到有一种方法可以创建一个values night和一个values文件夹。但是,由于我的启动屏幕,如何在启动前一晚从值更改为值。因为WhatsApp,我知道一定有一种方法可以产生一个以黑暗和光明为主题的闪屏 如何在显示闪屏前的晚上更改为值?尝试我正在使用的暗模式代码 步骤-1 首先创建night文件夹到您的资源文件中,如下图所示(即值night) 步骤-2 为夜间模式创建与下图相同的样式、字符串和颜色xml文件,并添加夜间模式应用时希望在应用程序中显示的夜间模式颜色、字符串和样式 为获得

我读到有一种方法可以创建一个values night和一个values文件夹。但是,由于我的启动屏幕,如何在启动前一晚从值更改为值。因为WhatsApp,我知道一定有一种方法可以产生一个以黑暗和光明为主题的闪屏


如何在显示闪屏前的晚上更改为值?

尝试我正在使用的暗模式代码

步骤-1

首先创建night文件夹到您的资源文件中,如下图所示(即值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>
<!-- 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>
<?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>
<?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>
步骤-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();
        }
    }
});
我希望这能帮助你


谢谢。

从AppCompat v1.1.0开始,setDefaultNightMode()会自动重新创建任何已启动的活动。
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();
        }
    }
});
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;
}