1. 夜間模式android
所謂的夜間模式,就是可以根據不一樣的設定,呈現不一樣風格的界面給用戶,並且晚上看着不傷眼睛。特別是一些新聞類App實現夜間模式是很是人性化的,加強用戶體驗。app
2. 我根據網上的資料 以及本身代碼親測,總結以下兩種方法:ide
(1)下降屏幕亮度佈局
(2)替換theme測試
3. 夜間模式之 下降屏幕亮度:this
(1)建立一個Android工程,命名爲"夜間模式_利用屏幕亮度(App)",以下:spa
(2)首先咱們來到主佈局之中,以下:.net
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" 6 tools:context="com.himi.screenlight.MainActivity" > 7 8 <Button 9 android:onClick="SetAppBrightness" 10 android:layout_width="match_parent" 11 android:layout_height="wrap_content" 12 android:text="設置應用程序內亮度" /> 13 14 </LinearLayout>
(3)來到MainActivity,以下:rest
1 package com.himi.screenlight; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.view.View; 6 import android.view.WindowManager; 7 /** 8 * 9 * 下降屏幕的亮度 10 * 該方法缺點是不能將亮度值保存起來,能夠經過SharedPreferences來保存數據。 11 * @author hebao 12 * 13 */ 14 public class MainActivity extends Activity { 15 16 private static int init = 1; 17 18 @Override 19 protected void onCreate(Bundle savedInstanceState) { 20 super.onCreate(savedInstanceState); 21 setContentView(R.layout.activity_main); 22 } 23 24 25 26 /****************設置應用程序的亮度*****************/ 27 public void SetAppBrightness(View view) { 28 switch(init % 5){ 29 case 0: 30 setBrightness(50); 31 32 break; 33 case 1: 34 setBrightness(100); 35 36 break; 37 case 2: 38 setBrightness(150); 39 40 break; 41 case 3: 42 setBrightness(200); 43 44 break; 45 case 4: 46 setBrightness(255); 47 48 break; 49 50 } 51 init ++; 52 } 53 54 55 public void setBrightness(int brightness) { 56 WindowManager.LayoutParams lp = getWindow().getAttributes(); 57 lp.screenBrightness = brightness / 255.0f; 58 getWindow().setAttributes(lp); 59 } 60 61 62 63 64 }
運行到手機上是能夠改變屏幕亮度的,這種方法實際開發中使用得很少。code
上面關於設置屏幕亮度的核心代碼以下:
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = brightness / 255.0f;
getWindow().setAttributes(lp);
4. 夜間模式之 替換theme:
(1)建立一個Android工程,命名爲"夜間模式_利用theme",以下:
(2)在工程 res/values/attrs.xml 文件中,增長自定義屬性。
1 <?xml version="1.0" encoding="utf-8"?> 2 <resources> 3 <declare-styleable name="NightMode"> 4 <attr name="day_night_background" format="color" /> 5 <attr name="day_night_text_color" format="color" /> 6 </declare-styleable> 7 </resources>
(3)在工程res/values/colors.xml文件中,增長自定義屬性用到的顏色。
1 <?xml version="1.0" encoding="utf-8"?> 2 <resources> 3 <color name="night_color">#000000</color> 4 <color name="light_color">#ffffff</color> 5 </resources>
(4)在工程 res/values/styles.xml 文件中,增長"AppSunTheme" 和"AppNightTheme",parent 均爲AppBaseTheme. 同時在這兩個styles中一一對應的加入attrs.xml文件中的屬性。
1 <resources> 2 3 <!-- 4 Base application theme, dependent on API level. This theme is replaced 5 by AppBaseTheme from res/values-vXX/styles.xml on newer devices. 6 --> 7 <style name="AppBaseTheme" parent="android:Theme.Light"> 8 <!-- 9 Theme customizations available in newer API levels can go in 10 res/values-vXX/styles.xml, while customizations related to 11 backward-compatibility can go here. 12 --> 13 </style> 14 15 <!-- Application theme. --> 16 <style name="AppTheme" parent="AppBaseTheme"> 17 <!-- All customizations that are NOT specific to a particular API-level can go here. --> 18 </style> 19 20 <!-- 自定義的 Application theme. --> 21 <style name="AppSunTheme" parent="AppBaseTheme"> 22 <item name="day_night_background">@color/light_color</item> 23 <item name="day_night_text_color">@color/night_color</item> 24 </style> 25 26 <style name="AppNightTheme" parent="AppBaseTheme"> 27 <item name="day_night_background">@color/night_color</item> 28 <item name="day_night_text_color">@color/light_color</item> 29 </style> 30 31 32 </resources>
(5)來到主佈局文件之中,將主佈局activity_main.xml中須要根據主題改變的元素的background 和 color 設爲自定義attrs中的屬性:
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:background="?attr/day_night_background" 6 android:paddingBottom="@dimen/activity_vertical_margin" 7 android:paddingLeft="@dimen/activity_horizontal_margin" 8 android:paddingRight="@dimen/activity_horizontal_margin" 9 android:paddingTop="@dimen/activity_vertical_margin" 10 tools:context=".MainActivity" > 11 12 <Button 13 android:id="@+id/btn_theme" 14 android:layout_width="match_parent" 15 android:layout_height="wrap_content" 16 android:layout_alignLeft="@+id/night" 17 android:layout_below="@+id/night" 18 android:layout_marginTop="28dp" 19 android:text="使用Theme實現夜間模式" 20 android:textColor="?attr/day_night_text_color" /> 21 22 </RelativeLayout>
(6)在工程中加入NightModeUtils類來配置應用主題。由於這裏要改變整個APP的主題,因此傳給NightModeUtils的Context應該是Application的Context。
1 package com.himi.screen; 2 3 import android.app.Activity; 4 import android.content.Context; 5 import android.content.Intent; 6 import android.content.SharedPreferences; 7 import android.view.View; 8 import android.widget.TextView; 9 10 public class NightModeUtils { 11 public final static int THEME_SUN = 1; 12 13 public final static int THEME_NIGHT = 2; 14 15 /** 16 * Set the theme of the Activity, and restart it by creating a new Activity 17 * of the same type. 18 */ 19 public static void changeToTheme(Activity activity) { 20 int theme1=getDayNightMode(activity); 21 int theme =( theme1 == THEME_SUN ? THEME_NIGHT : THEME_SUN); 22 setDayNightMode(activity, theme); 23 24 activity.finish(); 25 activity.startActivity(new Intent(activity, activity.getClass())); 26 } 27 28 /** Set the theme of the activity, according to the configuration. */ 29 public static void onActivityCreateSetTheme(Activity activity) { 30 int theme = getDayNightMode(activity); 31 switch (theme) { 32 case THEME_SUN: 33 activity.setTheme(R.style.AppSunTheme); 34 break; 35 case THEME_NIGHT: 36 activity.setTheme(R.style.AppNightTheme); 37 break; 38 default: 39 break; 40 } 41 } 42 43 public static void setBackGroundColor(Context context, View view, int theme) { 44 int color = context.getResources().getColor( 45 theme == THEME_SUN ? R.color.light_color : R.color.night_color); 46 view.setBackgroundColor(color); 47 } 48 49 public static void setTextColor(Context context, View view, int theme) { 50 int color = context.getResources().getColor( 51 theme == THEME_SUN ? R.color.night_color : R.color.light_color); 52 TextView textView = (TextView)view; 53 textView.setTextColor(color); 54 } 55 56 public static int getSwitchDayNightMode(Context context) { 57 int mode = getDayNightMode(context); 58 return mode == THEME_SUN ? THEME_NIGHT : THEME_SUN; 59 } 60 //設置theme屬性值存入xml文件之中 61 public static void setDayNightMode(Context context, int mode) { 62 SharedPreferences sharedPreferences = getSharedPreferences(context); 63 SharedPreferences.Editor sharedPreferencesEditor = sharedPreferences.edit(); 64 sharedPreferencesEditor.putInt("SUN_NIGHT_MODE", mode); 65 sharedPreferencesEditor.apply(); 66 } 67 //獲取xml文件中theme屬性值 68 public static int getDayNightMode(Context context) { 69 SharedPreferences sharedPreferences = getSharedPreferences(context); 70 return sharedPreferences.getInt("SUN_NIGHT_MODE", THEME_SUN); 71 } 72 //設置xml文件 73 private static SharedPreferences getSharedPreferences(Context context) { 74 return context.getSharedPreferences("NightModeDemo", Context.MODE_APPEND); 75 } 76 }
(7) 在每一個Activity中增長調用nightModeUtils類的設置主題方法,注意要加在setContentView方法以前。
setTheme方法只能在onCreate方法中實現,因此若是要改變當前Activity的注意要將當前Activity先finish,再從新啓動Activity。
1 package com.himi.screen; 2 3 import android.app.Activity; 4 import android.content.Context; 5 import android.content.Intent; 6 import android.os.Bundle; 7 import android.view.View; 8 import android.view.View.OnClickListener; 9 import android.widget.Button; 10 11 public class MainActivity extends Activity { 12 13 private Button btn_theme; 14 15 @Override 16 protected void onCreate(Bundle savedInstanceState) { 17 super.onCreate(savedInstanceState); 18 // 設置啓動時默認theme 19 NightModeUtils.onActivityCreateSetTheme(this); 20 setContentView(R.layout.activity_main); 21 22 btn_theme = (Button) findViewById(R.id.btn_theme); 23 24 btn_theme.setOnClickListener(new OnClickListener() { 25 26 @Override 27 public void onClick(View v) { 28 // 獲取當前theme 29 int theme = NightModeUtils.getDayNightMode(MainActivity.this); 30 31 Context context = getApplicationContext(); 32 33 if (theme == NightModeUtils.THEME_SUN) 34 NightModeUtils.setDayNightMode(context, 35 NightModeUtils.THEME_NIGHT); 36 else 37 NightModeUtils.setDayNightMode(context, 38 NightModeUtils.THEME_SUN); 39 40 // 注意改過主題後必定要,把activity finish在重開一遍,由於更改主題只能在oncreat中進行 41 finish(); 42 startActivity(new Intent(MainActivity.this, MainActivity.this.getClass())); 43 44 } 45 }); 46 } 47 48 49 }
注意的是:當咱們替換主題的時候,咱們須要finish掉舊Activity,從新使用startActivity開啓新Activity,這是由於咱們只是替換主題屬性,界面並無從新繪製,須要Activity從新渲染界面,因此須要根據新的屬性,從新繪製Activity。
(8)將Mainfest配置文件中Application 的theme設爲默認的AppSunTheme:
1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.himi.screen" 4 android:versionCode="1" 5 android:versionName="1.0" > 6 7 <uses-sdk 8 android:minSdkVersion="15" 9 android:targetSdkVersion="21" /> 10 11 <application 12 android:allowBackup="true" 13 android:icon="@drawable/ic_launcher" 14 android:label="@string/app_name" 15 android:theme="@style/AppSunTheme" > 16 <activity 17 android:name=".MainActivity" 18 android:label="@string/app_name" > 19 <intent-filter> 20 <action android:name="android.intent.action.MAIN" /> 21 22 <category android:name="android.intent.category.LAUNCHER" /> 23 </intent-filter> 24 </activity> 25 </application> 26 27 </manifest>
上面設置了應用程序初始狀態的主題樣式爲AppSunTheme
總結以下:
(9)部署程序到手機上測試,以下:
本文示例代碼下載地址:http://download.csdn.net/detail/hebao5201314/9591112