我正在我的应用程序中开发Day / Night功能,因此我阅读了这些文档并开始进行开发。
在白天或夜晚使用默认值都可以正常工作使用专用方法AppCompatDelegate.setDefaultNightMode(*)
。
用于自定义night主题颜色,我创建values-night文件夹,然后在其中创建colors.xml文件,如下所示。
res -> values -> colors.xml
res -> values-night -> colors.xml
我放置了该颜色但没有应用到Night主题之后!非常奇怪的是,为什么超值夜颜色未始终应用其显示的默认夜色?我研究了一些但找不到解决方案。
预先感谢。
根据我的早期答案,请遵循以下解决方案。我还在此答案中添加了style.xml。
https://stackoverflow.com/a/60038517/11158194
Step-1
首先创建night文件夹到资源文件中,如下图所示(即values-night)
Step-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> </resources>
values-night-> style.xml
<resources xmlns:tools="http://schemas.android.com/tools"> <!-- 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> </resources>
Step-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 }
Step-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 }
Step-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; }
我希望这可以帮助您!
谢谢。