//工程自带的values文件夹中
//colors.xml文件
<?xml version="1.0"encoding="utf-8"?>
<resources>
<colorname="colorPrimary">#3F51B5</color>
<colorname="colorPrimaryDark">#303F9F</color>
<colorname="colorAccent">#FF4081</color>
<colorname="main_textView">#FF0000</color>
</resources>
//工程自带的values文件夹中
//strings.xml文件
<resources>
//自己的项目名
<stringname="app_name">DayNightTwo</string>
<stringname="change_to_content">切换成夜间模式</string>
</resources>
//自己创建一个文件夹 values-night
将以上两个文件粘贴过来
//colors.xml文件中
<?xml version="1.0"encoding="utf-8"?>
<resources>
<colorname="colorPrimary">#35464e</color>
<colorname="colorPrimaryDark">#212a2f</color>
<colorname="colorAccent">#212a2f</color>
<colorname="main_textView">#00FF00</color>
</resources>
//strings.xml中
<resources>
<stringname="app_name">DayNightTwo</string>
<stringname="change_to_content">切换成日间模式</string>
</resources>
//activity_main中的布局设置
<?xml version="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.android_day_night.MainActivity"
android:gravity="center_horizontal"
>
<Button
android:text="@string/change_to_content"
android:id="@+id/change_theme_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:gravity="center_horizontal"
android:onClick="onClick" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:text="Hello World!"
android:textColor="@color/main_textView" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Hello World!"
android:textColor="@color/main_textView" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Hello World!"
android:textColor="@color/main_textView" />
</LinearLayout>
//创建一个自定义的Application在清单文件中配置
public class App extends Application {
@Override
public voidonCreate() {
super.onCreate();
if(getSharedPreferences("theme",MODE_PRIVATE).getBoolean("night_theme", false)) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
}
}
}
//MainActivity中
//按钮的点击监听事件
public void onClick(View v) {
//切换日夜间模式
int uiMode;
uiMode =getResources().getConfiguration().uiMode &Configuration.UI_MODE_NIGHT_MASK;
switch(uiMode) {
case Configuration.UI_MODE_NIGHT_YES:
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
getSharedPreferences("theme",MODE_PRIVATE).edit().putBoolean("night_theme", false).commit();
break;
caseConfiguration.UI_MODE_NIGHT_NO:
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
getSharedPreferences("theme",MODE_PRIVATE).edit().putBoolean("night_theme", true).commit();
break;
}
recreate();
}