这是其中一种实现模式,也是比较麻烦的一种,首先写布局,不多说上代码
activity_main.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.day_night_demo2.MainActivity"> <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
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>
-------------------------------------------------------------------------------------------------------
res资源文件夹下values文件夹下创建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
<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>
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Strings.xml
<string name="changge_to_night">切换成夜间模式</string>
<string name="changge_to_day">切换成日间模式</string>
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
styles.xml
<!-- 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>
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
在res文件夹下创建anim文件夹用来存放动画,为了使模式切换更自然,使用补间动画
sliding_in.xml
<?xml version="1.0" encoding="utf-8"?>
<alphaxmlns: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>
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
下面就是具体实现代码了
package com.example.day_night_demo2; import android.app.Activity;
import android.content.Intent; /**
* Created by lenovo on 2017/9/5.
*/
public class ThemeUtil {
//我当前的主题
private static int theme = ; //日间模式主题
private static final int DAY_THEME = ; //夜间模式主题
private static final int NIGHT_THEME = ; 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) {
//改变当前主题的theme变量
switch (theme) {
case DAY_THEME:
theme = NIGHT_THEME;
break;
case NIGHT_THEME:
theme = DAY_THEME;
break;
} //重启这个activity
activity.finish();
activity.overridePendingTransition(R.anim.sliding_in,R.anim.sliding_out);
activity.startActivity(new Intent(activity,activity.getClass()));
}
}
=========================================================================================
package com.example.day_night_demo2; import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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){
ThemeUtil.ChangeCurrentTheme(this);
}
}