1.在values中的colors里设置颜色的属性
两组属性一组日间的 一组夜间的<?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>
2.在style.xml中定义两种主题,也就是日间主题和夜间主题
<!--日间模式--> <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文件
attrs.xml:
<?xml version="1.0" encoding="utf-8"?> <resources> <attr name="mainBackground" format="color|reference"></attr> </resources>
4.需要夜间模式的布局引用attrs自定义属性
<!--在布局里引用我们自定义的attr资源android:background="?attr/mainBackground" 可以使用到此属性的控件带有日夜间模式切换-->
main_activity:
<?xml version="1.0" encoding="utf-8"?> <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="liguangjie.bawei.com.demo.MainActivity"> <!--这里引用我们自定义的attr资源 android:background="?attr/mainBackground" 可以使用到此属性的控件带有日夜间模式切换--> <!--android:background="?attr/mainBackground"--> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="日夜模式切换" /></RelativeLayout>
5.Java代码
MainActivity:
import android.os.PersistableBundle; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private int theme = R.style.AppTheme; private TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //判断是否有主题存储 if (savedInstanceState != null) { // theme= savedInstanceState.getInt("theme"); //更改主题 setTheme(theme); } setContentView(R.layout.activity_main); tv = (TextView) findViewById(R.id.tv); tv.setOnClickListener(this); } @Override public void onClick(View view) { //这里用到了三元运算符 比if判断更简洁 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; // } Toast.makeText(this, "模式切换", Toast.LENGTH_SHORT).show(); //重新载入 recreate(); } //存入主题 @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putInt("theme", theme); } //取出主题 @Override public void onRestoreInstanceState(Bundle savedInstanceState, PersistableBundle persistentState) { super.onRestoreInstanceState(savedInstanceState, persistentState); savedInstanceState.getInt("theme"); } }