編輯:關於 Android 程式設計
專案上遇到兩個相關需求:狀態欄要透明,且當使用者拖動、頂部內容滾動時,標題欄顏色要能跟著變化。這類效果常被稱為「沉浸式狀態欄」(這個叫法網路上有不少討論,這裡統一稱之為狀態欄著色,比較貼近實作內容)。
從 Android 4.4 開始才支援狀態欄著色,5.0 之後 API 更完整,可以直接在主題裡寫 android:colorPrimaryDark,或用 getWindow().setStatusBarColor(color) 設定。但 4.4 以上的裝置仍佔相當比例,因此需要一套向下相容的做法。
這個方案不做 fitsSystemWindows,而是讓內容填滿狀態欄區域,再自行安排頂部留白。彈性最高,特別適合「狀態欄顏色需跟隨 Fragment 切換」這類場景。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// 5.0 及以上
View decorView = getWindow().getDecorView();
int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
decorView.setSystemUiVisibility(option);
getWindow().setStatusBarColor(Color.TRANSPARENT);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
// 4.4 ~ 5.0
WindowManager.LayoutParams localLayoutParams = getWindow().getAttributes();
localLayoutParams.flags = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
| localLayoutParams.flags;
}
放在 Activity 或基類中執行即可。除了程式碼,也可以用主題達成同樣效果,在 values、values-v19、values-v21 三個目錄分別定義:
<!-- values -->
<style name="TranslucentTheme" parent="AppTheme" />
<!-- values-v19 -->
<style name="TranslucentTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">false</item>
</style>
<!-- values-v21 -->
<style name="TranslucentTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">false</item>
<item name="android:statusBarColor">@android:color/transparent</item>
</style>
幫 Activity 或 Application 掛上這個主題即可。兩種方式依需求挑一種,不衝突。
透明化之後直接執行,會看到版面頂到狀態欄底下。這正是我們要的——接下來只要讓頂部元件補上等高的留白,顏色自然就上去了。
先在 values 與 values-v19 各定義一個高度:
<!-- values --> <dimen name="padding_top">0dp</dimen> <!-- values-v19 --> <dimen name="padding_top">25dp</dimen>
這裡的 25dp 在部分客製系統上會有誤差,若要求精準,建議改用 getStatusBarHeight() 動態計算並以程式設定 padding。
最常見的情況是狀態欄與 Toolbar 同色。既然版面已經延伸到狀態欄,只要給 Toolbar 加上一個狀態欄高度的 top padding:
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/padding_top"
app:contentInsetStart="0dp" />

自定義 title 的情況經測試也可行,做法相同。
先給 Toolbar 加上 top padding,在 4.4 上看,NavigationView 並沒有延伸到狀態欄。這時在 5.0 以下補一段設定:
private void navViewToTop() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
mDrawerLayout.setFitsSystemWindows(true);
mDrawerLayout.setClipToPadding(false);
}
}

這是最省事的情況,因為版面已經延伸,圖片自然會填到狀態欄底下,不需要任何額外處理:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="@mipmap/top_image" />
</LinearLayout>

這種頁面常有「每個 Fragment 頂部顏色不同」的需求。原理與 2.1 相同——每個 Fragment 用自己的 title 並補上 paddingTop,顏色就各自獨立了。以 FragmentOne 為例:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ff9900"
android:paddingTop="@dimen/padding_top">
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:text="fragment one" />
</LinearLayout>
</LinearLayout>
FragmentTwo 結構相同,只把背景換成藍色。切換效果:

顏色跟隨切換、沒有延遲感。這個做法比早期常見的「在 Activity 層手動切換狀態欄顏色」自然得多,後者容易出現過渡不順的問題。
這次反過來,用 android:fitsSystemWindows="true" 讓版面不要延伸,狀態欄保持透明,再手動塞一個等高同寬的 View 去蓋住它。
第一步一樣先透明化,第二步在根布局加上屬性:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">
</LinearLayout>
第三步,動態建立狀態欄 View 並加入內容區:
private void addStatusBarView() {
View view = new View(this);
view.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight(this));
ViewGroup decorView = findViewById(android.R.id.content);
decorView.addView(view, params);
}

原理很簡單,缺點是每個頁面都得額外寫一次這段程式碼。
同樣使用 android:fitsSystemWindows="true",但改成直接把根布局的背景設成狀態欄顏色。因為根布局整塊都被染色,內部需要多包一層 Layout 來指定真正的內容背景(例如白色),否則整個畫面都會變成狀態欄色。
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff9900"
android:fitsSystemWindows="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical">
<!-- 實際內容 -->
</LinearLayout>
</RelativeLayout>

看起來乾淨,但版面嵌套多一層。若專案有幾十個頁面,一頁一頁改寫的同時還要留意嵌套層級會不會影響效能與滾動行為,成本並不低。
三個方案各有適用範圍:
另外補充一點:4.4 的 FLAG_TRANSLUCENT_STATUS 是「模擬透明」,系統會在半透明狀態欄上蓋一層灰階遮罩,無法做到真正的全透明或自訂顏色。若設計稿要求精準還原,5.0 以上的 setStatusBarColor() 才是完整解法,4.4 只能當作降級方案對待。
Android 小火箭發射動畫實作解析
這個案例以「小火箭發射」為主題,展示如何結合服務、透明 Activity、WindowManager 與動畫,完成一個可拖曳並發射的互動效果。 整體架構 火箭本體由一個背
Android 傚網易客戶端頂部導航欄:滑動指示條與左右捲動實作
本文分享一個網易新聞風格的首頁頂部導航列動畫,並說明它的核心原理與實作細節。 版面結構 先來看布局。這份結構參考自網易客戶端的反編譯結果,理解之後不難改寫: <
Android 自定義支付密碼軟鍵盤實作(含隨機鍵盤)
Android 自定義輸入支付密碼的軟鍵盤 專案需要實現一個類似支付寶那樣的密碼鎖功能,帶有自己的軟鍵盤。這裡整理了相關資料供大家參考,若有錯誤歡迎留言指正。 需求:實現
Android SpannableString 複合文本實戰
當需要在一個 TextView 中同時呈現不同顏色、字體大小、底線、刪除線、圖片或點擊事件時,SpannableString 是非常實用的工具。 常見 Span 前景色