一、使用 setTheme 方法
在 colors.xml 中定义两组颜色,分别表示日间和夜间的主题色:
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="colorPrimary">#3F51B5</color> <color name="colorPrimaryDark">#303F9F</color> <color name="colorAccent">#FF4081</color> <color name="nightColorPrimary">#3b3b3b</color> <color name="nightColorPrimaryDark">#383838</color> <color name="nightColorAccent">#a72b55</color> </resources>
之后在 styles.xml 中定义两组主题,也就是日间主题和夜间主题:
<!-- 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="NightAppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/nightColorPrimary</item> <item name="colorPrimaryDark">@color/nightColorPrimaryDark</item> <item name="colorAccent">@color/nightColorAccent</item> <item name="android:textColor">@android:color/white</item> <item name="android:background">#303030</item> <item name="mainBackground">@color/nightColorPrimaryDark</item> </style> 在主题中的
mainBackground
属性是我们自定义的属性,用来表示背景色:<resources> <attr name="mainBackground" format="color|reference"></attr> </resources>
最后就是MainActivity
的代码:
public
class
MainActivity
extends
AppCompatActivity {
// 默认是日间模式
private
int
theme = R.style.AppTheme;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
// 判断是否有主题存储
if
(savedInstanceState !=
null
){
theme = savedInstanceState.getInt(
"theme"
);
setTheme(theme);
}
setContentView(R.layout.activity_main);
Button btn_theme = (Button) findViewById(R.id.btn_theme);
btn_theme.setOnClickListener(
new
View.OnClickListener() {
@Override
public
void
onClick(View v) {
theme = (theme == R.style.AppTheme) ? R.style.NightAppTheme : R.style.AppTheme;
MainActivity.
this
.recreate();
}
});
}
@Override
protected
void
onSaveInstanceState(Bundle outState) {
super
.onSaveInstanceState(outState);
outState.putInt(
"theme"
, theme);
}
@Override
protected
void
onRestoreInstanceState(Bundle savedInstanceState) {
super
.onRestoreInstanceState(savedInstanceState);
theme = savedInstanceState.getInt(
"theme"
);
}
}
二、使用 Android Support Library 中的 UiMode 方法
下面是 values/colors.xml :<?xml version="1.0" encoding="utf-8"?> <resources> <color name="colorPrimary">#3F51B5</color> <color name="colorPrimaryDark">#303F9F</color> <color name="colorAccent">#FF4081</color> <color name="textColor">#FF000000</color> <color name="backgroundColor">#FFFFFF</color> <color name="windowBackground">#fff</color> </resources>除了 values/colors.xml 之外,我们还要创建一个 values-night/colors.xml 文件,
用来设置夜间模式的颜色,其中 <color> 的 name 必须要和 values/colors.xml
中的相对应:
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="colorPrimary">#3b3b3b</color> <color name="colorPrimaryDark">#383838</color> <color name="colorAccent">#a72b55</color> <color name="textColor">#FFFFFF</color> <color name="backgroundColor">#3b3b3b</color> <color name="windowBackground">#000</color> </resources>在 styles.xml 中去引用我们在 colors.xml 中定义好的颜色:
<resources> <!-- 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> <item name="android:textColor">@color/textColor</item> <item name="mainBackground">@color/backgroundColor</item> <item name="android:windowBackground">@color/windowBackground</item> </style>在 MyApplication 中先选择一个默认的 Mode :
public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); // 默认设置为日间模式 AppCompatDelegate.setDefaultNightMode( AppCompatDelegate.MODE_NIGHT_NO); } }
要注意的是,这里的 Mode 有四种类型可以选择:
1、MODE_NIGHT_NO: 使用亮色(light)主题,不使用夜间模式;
2、MODE_NIGHT_YES:使用暗色(dark)主题,使用夜间模式;
3、MODE_NIGHT_AUTO:根据当前时间自动切换 亮色(light)/暗色(dark)主题;
4、MODE_NIGHT_FOLLOW_SYSTEM(默认选项):设置为跟随系统,通常为 MODE_NIGHT_NO
当用户点击按钮切换日/夜间时,重新去设置相应的 Mode :
public class MainActivity extends AppCompatActivity { private TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv=(TextView)findViewById(R.id.tv); tv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int currentNightMode = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK; getDelegate().setLocalNightMode(currentNightMode == Configuration.UI_MODE_NIGHT_NO ? AppCompatDelegate.MODE_NIGHT_YES : AppCompatDelegate.MODE_NIGHT_NO); // 同样需要调用recreate方法使之生效 recreate(); } }); } } ps:1.colorPrimary 应用的主要色调,actionBar默认使用该颜色,Toolbar导航栏的底色
2.colorPrimaryDark 应用的主要暗色调,statusBarColor默认使用该颜色
3.statusBarColor 状态栏颜色,默认使用colorPrimaryDark
4.windowBackground 窗口背景颜色
5.navigationBarColor 底部栏颜色
6.colorForeground 应用的前景色,ListView的分割线,switch滑动区默认使用该颜色
7.colorBackground 应用的背景色,popMenu的背景默认使用该颜色
8.colorAccent CheckBox,RadioButton,SwitchCompat等一般控件的选中效果默认采用该颜色
9.colorControlNormal CheckBox,RadioButton,SwitchCompat等默认状态的颜色。
10.colorControlHighlight 控件按压时的色调
11.colorControlActivated 控件选中时的颜色,默认使用colorAccent
12.colorButtonNormal 默认按钮的背景颜色
13.editTextColor:默认EditView输入框字体的颜色。
14.textColor Button,textView的文字颜色
15.textColorPrimaryDisableOnly RadioButton checkbox等控件的文字
16.textColorPrimary 应用的主要文字颜色,actionBar的标题文字默认使用该颜色
17.colorSwitchThumbNormal: switch thumbs 默认状态的颜色. (switch off)