編輯:關於 Android 程式設計
智慧型手機迅速普及,也大幅改變了我們的生活習慣。很多人習慣睡前滑一會兒手機,但一般應用的日間模式亮度偏高,對眼睛其實是種負擔。因此現在的應用多半會提供「日間/夜間」兩套模式讓使用者切換。那這種切換究竟怎麼實作?
以文字閱讀為主的 App 幾乎都會有夜間模式,能依設定呈現不同風格的介面,晚上看起來也不刺眼。本質上就是所謂的換膚(主題切換)。網路上流傳的做法很多,本文先分享一種最取巧的思路:透過設定一個半透明覆蓋層,降低螢幕的亮度與色度。

public void night() { WindowManager.LayoutParams params = new WindowManager.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, LayoutParams.TYPE_APPLICATION, WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT); params.gravity=Gravity.BOTTOM; params.y=10; if(myView==null){ myView=new TextView(this); myView.setBackgroundColor(0x80000000); } mWindowManager.addView(myView, params); Editor edit = skinSp.edit(); edit.putString("skin", NIGHT); edit.commit(); }
public void day(){ if(myView!=null){ mWindowManager.removeView(myView); Editor edit = skinSp.edit(); edit.putString("skin", DAY); edit.commit(); } }
關鍵在 FLAG_NOT_TOUCHABLE 與 FLAG_NOT_FOCUSABLE 這兩個旗標:蓋上去之後不能攔截觸控與焦點,否則整個介面就點不動了。這種做法改動最小,但終究只是「調暗」,並非真正換色。
接著看更完整的做法。


上面兩張圖正是兩款應用的夜間模式。GitHub 上已有不少 PluginTheme 之類的開源方案,多數時候直接拿来用就好,但比起會用輪子,我更想講清楚輪子怎麼造。
換膚目前比較流行的做法是插件化動態加載:皮膚可以熱插拔,使用者按需下載、不用就刪,既不會讓 apk 因為一堆圖片而膨脹,也不影響宿主 App 的功能。這只是插件化技術的冰山一角,有興趣可以看看 360 開源的 DroidPlugin、OpenAltas 框架,以及 DL 框架。
回到主題切換本身。所謂主題切換,換的是整個 App 的配色、排版與字型風格,通常不會有大量圖片替換;若換的是大量圖片,那就屬於換膚的範疇了。
效果如下:

切到夜間模式時,背景色、字體顏色、按鈕色、標題欄色統統要換,切回白天再復原。做法是這樣的。
白天主題
<resources> <style name="DayTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">#03A9F4</item> <item name="android:textColorPrimary">#ffffff</item> <item name="android:windowBackground">@color/background_material_light</item> <item name="colorAccent">#00BCD4</item> <item name="colorControlNormal">#00BCD4</item> <item name="titleStyle">@style/DayTitleStyle</item> <item name="contentStyle">@style/DayContentStyle</item> <item name="buttonBg">#2196F3</item> <item name="buttonTextColor">#ffffff</item> <item name="checkTextColor">#2196F3</item> <item name="switchTextColor">#2196F3</item> </style> <style name="DayTitleStyle"> <item name="android:textColor">#212121</item> <item name="android:textSize">20sp</item> <item name="android:layout_margin">8dp</item> </style> <style name="DayContentStyle"> <item name="android:textColor">#9C27B0</item> <item name="android:textSize">16sp</item> <item name="android:layout_margin">16dp</item> <item name="android:maxLines">10</item> </style> </resources>
夜間主題
<resources> <style name="NightTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">#00796B</item> <item name="android:textColorPrimary">#212121</item> <item name="android:windowBackground">@color/background_material_dark</item> <item name="colorAccent">#00796B</item> <item name="colorControlNormal">#212121</item> <item name="titleStyle">@style/NightTitleStyle</item> <item name="contentStyle">@style/NightContentStyle</item> <item name="buttonBg">#00796B</item> <item name="buttonTextColor">#9E9E9E</item> <item name="checkTextColor">#212121</item> <item name="switchTextColor">#212121</item> </style> <style name="NightTitleStyle"> <item name="android:textColor">#212121</item> <item name="android:textSize">20sp</item> <item name="android:layout_margin">8dp</item> </style> <style name="NightContentStyle"> <item name="android:textColor">#212121</item> <item name="android:textSize">16sp</item> <item name="android:layout_margin">16dp</item> <item name="android:maxLines">10</item> </style> </resources>
兩套主題的屬性名稱完全一致,只有值不同。其中 DayTheme 與 NightTheme 裡有這一段:
<item name="titleStyle">@style/DayTitleStyle</item> <item name="contentStyle">@style/DayContentStyle</item> <item name="buttonBg">#2196F3</item> <item name="buttonTextColor">#ffffff</item> <item name="checkTextColor">#2196F3</item> <item name="switchTextColor">#2196F3</item>
style 裡正常是不會有這些屬性的,它們都是自定義屬性,用來控制特定控件或版面的外觀,宣告在 attr 檔中:
<?xml version="1.0" encoding="utf-8"?> <resources> <attr name="contentStyle" format="reference"/> <attr name="titleStyle" format="reference"/> <attr name="buttonBg" format="reference|color"/> <attr name="buttonTextColor" format="reference|color"/> <attr name="checkTextColor" format="reference|color"/> <attr name="switchTextColor" format="reference|color"/> </resources>
在版面配置中引用:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/title" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="CheckBox" android:textColor="?attr/checkTextColor" /> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="CheckBox" android:textColor="?attr/checkTextColor" /> <Switch android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Switch" android:textColor="?attr/switchTextColor" /> <Button android:id="@+id/btn_setting" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="設置" android:background="?attr/buttonBg" android:textColor="?attr/buttonTextColor" /> </LinearLayout>
最後在 App 主題的 style 中替它們賦值即可。這樣做的好處是:整個 App 只掛一個 style,而個別控件若想與主題不同,只要透過自定義屬性在主題中給不同值,不必逐一硬寫顏色。
有了兩套主題,接下來用 setTheme(R.style.*) 切換。但這個方法只在 setContentView() 之前呼叫才有效,所以想在別的地方切換是行不通的,於是衍生兩個問題:
1、如何讓「目前這個設定頁」在切換後立刻換風格?
2、如何讓「先前已經開啟的頁面」也跟著換?
我的解法是:
1、在目前頁面呼叫 Activity.recreate()。此方法會銷毀並重建當前的 Activity 實例,等於重新走一次 onCreate,setTheme 自然生效。
2、透過 Intent 的 Flag 讓先前開啟的頁面重建:設定 mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK),清除舊的 Activity 再建立新的,返回時就是新主題了。若頁面較多,可把 MainActivity 的 launchMode 設為 singleTask,返回主頁時一併清掉其他頁面來更新主題。
用一個工具類統一控制,並在每個 Activity 的 setContentView() 之前呼叫:
public class ThemeChangeUtil { public static boolean isChange = false; public static void changeTheme(Activity activity){ if(isChange){ activity.setTheme(R.style.NightTheme); } } }
設定頁:
public class ChangeTheme extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { ThemeChangeUtil.changeTheme(this); super.onCreate(savedInstanceState); setContentView(R.layout.activity_change); Button mChangeBtn = (Button) findViewById(R.id.btn_change); mChangeBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (ThemeChangeUtil.isChange) { ThemeChangeUtil.isChange = false; } else { ThemeChangeUtil.isChange = true; } ChangeTheme.this.recreate();//重新創建當前Activity實例 } }); } @Override public void onBackPressed() { super.onBackPressed(); Intent mIntent = new Intent(this, MainActivity.class); mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); startActivity(mIntent); finish(); } }
主頁:
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { ThemeChangeUtil.changeTheme(this); super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button mSettingBtn = (Button) findViewById(R.id.btn_setting); mSettingBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { MainActivity.this.startActivity(new Intent(MainActivity.this, ChangeTheme.class)); } }); } }
最後補一個實務建議:把 isChange 這種全域旗標改成用 SharedPreferences 持久化會更穩妥,因為 App 進程被系統回收後,靜態變數會歸零,使用者會發現夜間模式「自己跳回白天」。把選擇存起來、啟動時讀回,才是完整的做法。
Android 中子線程更新 UI 的三種 Handler 寫法:從匿名類到消息機制
寫 Android 遲早會撞到這條規則:子線程不能直接改寫介面。要繞過它,最基礎也最經典的工具就是 Handler。本文把三種常見寫法放在一起對照,順帶說清它們背後其實是
Android 4.4 KitKat Phone 工作流程淺析(十一):PSensor 近距離感測器原理剖析
本文出自 https://blog.csdn.net/yihongyuelan ,轉載請務必保留出處。 概要 在 Android 手機的通話過程中,使用者把手機靠近或遠離
Android Service 組件详解:startService 與 bindService 的用法
一、Service 簡介 服務(Service)是 Android 中用於在後台執行任務的組件,非常適合執行那些不需要與使用者互動、但需要長期運行的操作,其運行不依賴任何
Android NDK 開發環境建置流程
新版 NDK 已大幅簡化開發環境設定,不再一定需要手動安裝 Cygwin。過去使用 NDK 時,常需下載 NDK、Cygwin、CDT 與設定編譯器環境變數,但新版本已整