main.xml的布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="?attr/mainBackground"
tools:context="com.example.night.MainActivity">
<!--注意:这里引用我们自定义的attr资源android:background="?attr/mainBackground"
可以使用到此属性的控件带有日夜模式切换-->
<TextView
android:background="?attr/mainBackground"
android:id="@+id/bt_change"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="日夜模式切换"
android:layout_centerInParent="true"
/>
</RelativeLayout>
Main里面的代码
/*
第一种设置夜间模式:通过重新给activity设置主题,然后销毁activity再创建activity,重新设置的主题才有效
1.在values中的colors里设置颜色属性
<!--白天模式下的颜色-->
<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>
2.在style.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>
<item name="android:textColor">@android:color/black</item>
<item name="mainBackground">@android:color/white</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="mainBackground">@color/nightColorPrimaryDark</item>
</style>
3.创建attrs.xml文件
<!--自定义的属性方便引用-->
<attr name="mainBackground" format="color|reference"></attr>
4.需要夜间模式的布局引用attr自定义属性
5.java代码
a.判断存储的类型
b.点击事件,根据类型进行日夜的切换
*/
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private TextView bt_change;
//默认是日间模式
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);
//设计点击事件
bt_change=(TextView)findViewById(R.id.bt_change);
bt_change.setOnClickListener(this);
}
//根据点击事件,完成日间模式切换的代码.
@Override
public void onClick(View view) {
switch (view.getId()){
//点击,完成日间模式切换代码
case R.id.bt_change:
//切换日夜模式代码,三元运算符比if else效率更高
theme = (theme == R.style.AppTheme) ? R.style.NightAppTheme : R.style.AppTheme;
//重新创建
recreate();
break;
default:
break;
}
}
//存theme属性
//这里需要特别注意这两个方法的区别 照片我已上传 一定要用第一个方法
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("theme",theme);
}
@Override
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
super.onSaveInstanceState(outState, outPersistentState);
outState.putInt("theme",theme);
}
//取theme属性 @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); theme = savedInstanceState.getInt("theme"); }}
希望大家多多支持,评论!!!