int mode = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK; switch (mode) { case Configuration.UI_MODE_NIGHT_NO: AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES); break; case Configuration.UI_MODE_NIGHT_YES: AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO); break; } recreate();
如果需要设置字体颜色和设置文字的话需要在res下面建立values-night文件夹,把strings和colors两个文件复制到你新建立的values-night下面设置里面的属性,如果设置图片的话,就同样在drawable或者mipmap-xhdpi建立一个drawable-night或mipmap-night-xhdpi文件夹,存放相同名字的图片就可以了.