安卓日夜模式的切换

来源:互联网 发布:淘宝广告直通车 编辑:程序博客网 时间:2024/04/30 01:04
1.在values中的colors里设置颜色的属性
<!--白天模式下的颜色-->
<colorname="colorPrimary">#3F51B5</color>
<colorname="colorPrimaryDark">#303F9F</color>
<colorname="colorAccent">#FF4081</color>
<!--夜晚模式下的颜色-->
<colorname="nightColorPrimary">#3b3b3b</color>
<colorname="nightColorPrimaryDark">#383838</color>
<colorname="nightColorAccent">#a72b55</color>

2.在style.xml中定义两组主题,也就是夜间主题和日间主题
<!-- Base application theme.日间主题-->
<stylename="AppTheme"parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<itemname="colorPrimary">@color/colorPrimary</item>
<itemname="colorPrimaryDark">@color/colorPrimaryDark</item>
<itemname="colorAccent">@color/colorAccent</item>
<itemname="android:textColor">@android:color/black</item>
<itemname="mainBackground">@android:color/white</item>
</style>
<!--夜间主题-->
<stylename="NightAppTheme"parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<itemname="colorPrimary">@color/nightColorPrimary</item>
<itemname="colorPrimaryDark">@color/nightColorPrimaryDark</item>
<itemname="colorAccent">@color/nightColorAccent</item>
<itemname="android:textColor">@android:color/white</item>
<itemname="mainBackground">@color/nightColorPrimaryDark</item>
</style>

3.创建attrs.xml文件
<!--自定义的属性方便引用-->
<attrname="mainBackground"format="color|reference"/>

4.需要夜间模式的布局引用attr自定义属性
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/mainBackground">
<!--这里引用一下我们自定义的atter资源android:background="?attr/mainBackground",
可以使用到此属性的控件带有日夜间模式切换-->
<TextView
android:id="@+id/bt_change"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?attr/mainBackground"
android:text="日夜模式切换"/>
</RelativeLayout>

5.java代码的使用
a.判断存储的类型,
b.根据点击事件,根据类型进行日夜间的切换
public classMainActivityextendsAppCompatActivityimplementsView.OnClickListener {

privateTextViewbt_change;
//默认是日间模式
private inttheme= R.style.AppTheme;

@Override
protected voidonCreate(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 voidonClick(View view) {
switch(view.getId()) {
//根据点击事件,完成日夜切换切换的代码
caseR.id.bt_change:
//切换日夜模式代码,三元运算符比if,else效率高
theme= (theme== R.style.AppTheme) ? R.style.NightAppTheme: R.style.AppTheme;
/*if (theme == R.style.AppTheme){
theme=R.style.NightAppTheme;
}else{
theme=R.style.AppTheme;
}*/
//重新创建
recreate();
break;
default:
break;
}
}
//
@Override
protected voidonSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("theme",theme);
}

//
@Override
protected voidonRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
theme= savedInstanceState.getInt("theme");
}
}
原创粉丝点击