在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="nightColorPrimary">#3b3b3b</color> <color name="nightColorPrimaryDark">#383838</color> <color name="nightColorAccent">#a72b55</color> </resources>
然後在values裏的styles.xml裏
<resources> <!-- 日間模式 --> <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> </resources>
然後在values裏創建attrs.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <!--reference 引用--> <attr name="mainBackground" format="color|reference"></attr> </resources>在相對佈局中或者線性佈局中添加:
android:background="?attr/mainBackground"MainActivity裏
import android.content.SharedPreferences; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { private SharedPreferences sharedPreferences; private SharedPreferences.Editor edit; private Button button; //默認的日間模式 private int theme = R.style.AppTheme; @Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);//方法1 //恢復數據 做判空
if (savedInstanceState != null) {
theme = savedInstanceState.getInt("theme");
//設置主題 此方法必須在setContentView()之前調用
setTheme(theme);
}
// //方法2
// sharedPreferences = getSharedPreferences("jiangjun",MODE_PRIVATE);
// edit = sharedPreferences.edit();
// boolean riye = sharedPreferences.getBoolean("riye", false);
// if (riye){
// setTheme(R.style.NightAppTheme);// }else {// setTheme(R.style.AppTheme);// } setContentView(R.layout.activity_main); button = (Button) findViewById(R.id.riye); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //方法1 //切換日夜間模式 theme = (theme == R.style.AppTheme) ? R.style.NightAppTheme : R.style.AppTheme;// //方法2// boolean riye1 = sharedPreferences.getBoolean("riye", false);// if (riye1){// edit.putBoolean("riye",false);// edit.commit();// }else {// edit.putBoolean("riye",true);// edit.commit();// } //重新創建 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"); }}