Mainactivity
package com.example.gitshiyan01;
import android.content.res.Configuration;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatDelegate;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//设置对应的主题 ,在ui创建好之后设置主题无效,所以要放到setContentView()方法前面setTheme()
ThemeUtil.onActivityCreatedSetTheme(this);
setContentView(R.layout.activity_main);
}
/*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;
case Configuration.UI_MODE_NIGHT_NO:
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
getSharedPreferences("theme", MODE_PRIVATE).edit().putBoolean("night_theme", true).commit();
break;
}
recreate();
}*/
public void onClick(View v) {
//切换日夜间模式
ThemeUtil.ChangeCurrentTheme(this);
}
}
Mainactivity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns: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.gitshiyan01.MainActivity">
<Button
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"
android:text="?attr/textContent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:text="Hello World!"
android:textColor="?attr/textColorValue" />
<TextView
android:textColor="?attr/textColorValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Hello World!" />
<TextView
android:textColor="?attr/textColorValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Hello World!" />
</LinearLayout>
ThemeUtil.java
package com.example.gitshiyan01;
import android.app.Activity;
import android.content.Intent;
/**
* Created by hasee on 2017/8/7.
*/
public class ThemeUtil {
//我当前应用的主题
private static int theme = 0;
//日间模式主题
private static final int DAY_THEME = 0;
//夜间模式主题
private static final int NIGHT_THEME = 1;
public static void onActivityCreatedSetTheme(Activity activity) {
switch (theme) {
case DAY_THEME:
activity.setTheme(R.style.day_theme);
break;
case NIGHT_THEME:
activity.setTheme(R.style.night_theme);
break;
}
}
//点击按钮改变对应得主题
public static void ChangeCurrentTheme(Activity activity) {
//1、改变当前主题的theme变量
switch (theme) {
case DAY_THEME:
theme = NIGHT_THEME;
break;
case NIGHT_THEME:
theme = DAY_THEME;
break;
}
//2、重启这个activity
activity.finish();
activity.overridePendingTransition(R.anim.sliding_in, R.anim.sliding_out);
activity.startActivity(new Intent(activity, activity.getClass()));
}
}
在res目录下创建anim文件夹
sliding_in.xml
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromAlpha="0.0"
android:toAlpha="1.0"></alpha>
sliding_out.xml
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:toAlpha="0.0"
android:fromAlpha="1.0"></alpha>
在values目录下创建attrs
attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="textColorValue" format="color"></attr>
<attr name="textContent" format="string"></attr>
</resources>
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="background">#252a2e</color>
<color name="unablebtn">#dcdcdc</color>
<color name="dark_bg">#505050</color>
<color name="light">#ECECEC</color>
<color name="white">#FFFFFF</color>
<color name="black">#000000</color>
<color name="green">#05D992</color>
<color name="zise">#E5004F</color>
<color name="dark_bg1">#414141</color>
<color name="pink">#FF5877</color>
<color name="yellow">#FFFF00</color>
</resources>
strings.xml
<resources>
<string name="app_name">DayNightOne</string>
<string name="changge_to_night">切换成夜间模式</string>
<string name="changge_to_day">切换成日间模式</string>
</resources>
styles.xml
<resources>
<!-- Base application theme. 白天的模式 -->
<style name="day_theme" 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:windowBackground">@color/white</item>
<!--日间模式对应的字体颜色 和日间模式对应的文本内容-->
<item name="textColorValue">@color/black</item>
<item name="textContent">@string/changge_to_night</item>
</style>
<!-- Base application theme. 夜晚的模式 -->
<style name="night_theme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/dark_bg</item>
<item name="colorPrimaryDark">@color/dark_bg</item>
<item name="colorAccent">@color/dark_bg</item>
<item name="android:windowBackground">@color/dark_bg</item>
<!--夜间模式对应的字体颜色 和夜间模式对应的文本内容-->
<item name="textColorValue">@color/white</item>
<item name="textContent">@string/changge_to_day</item>
</style>
</resources>
最后在清单文件了面修改 android:theme="@style/day_theme">