Android 日夜间切换Demo
這是其中一種實現(xiàn)模式,也是比較麻煩的一種,首先寫布局,不多說上代碼
activity_main.xml
res資源文件夾下values文件夾下創(chuàng)建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><!--日間模式對應(yīng)的字體顏色 和日間模式對應(yīng)的文本內(nèi)容--> <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><!--夜間模式對應(yīng)的字體顏色 和夜間模式對應(yīng)的文本內(nèi)容--> <item name="textColorValue">@color/white</item><item name="textContent">@string/changge_to_day</item> </style>--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
在res文件夾下創(chuàng)建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>?
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
?
下面就是具體實現(xiàn)代碼了
package com.example.day_night_demo2;import android.app.Activity; import android.content.Intent;/** * Created by lenovo on 2017/9/5. */ public class ThemeUtil {//我當(dāng)前的主題 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;}}//點擊按鈕改變對應(yīng)的主題 public static void ChangeCurrentTheme(Activity activity) {//改變當(dāng)前主題的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);//設(shè)置對應(yīng)的主題 ,在ui創(chuàng)建好之后設(shè)置主題無效,所以要放到setContentView()方法前面setTheme() ThemeUtil.onActivityCreatedSetTheme(this);setContentView(R.layout.activity_main);}public void onClick(View v){ThemeUtil.ChangeCurrentTheme(this);} }?
?
轉(zhuǎn)載于:https://www.cnblogs.com/powersen/p/7524584.html
總結(jié)
以上是生活随笔為你收集整理的Android 日夜间切换Demo的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Day-10: 错误、调试和测试
- 下一篇: Vue收藏资料