編輯:關於 Android 程式設計
這個特性從 Android 4.4 開始支援,最低需要 API 19。做法出乎意料地簡單,但真正要用得好看,還有幾個細節要處理。
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 透明狀態欄
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
// 透明導航欄
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
}
加上這兩行,系統的狀態列與導航列就變成半透明,應用內容會延伸到下方。效果如下:

版面是一個很單純的垂直 LinearLayout:
<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:background="#ffffff"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#009959" />
<Button
android:layout_width="100dp"
android:layout_height="50dp"
android:background="#ff669d" />
</LinearLayout>
但一旦在綠色的 TextView 裡塞入文字:

文字直接跟狀態欄重疊了。因為內容已經向上延伸,系統不會自動幫你避開。
android:fitsSystemWindows="true" android:clipToPadding="true"
<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:background="#ffffff"
android:clipToPadding="true"
android:fitsSystemWindows="true"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#009959" />
<Button
android:layout_width="100dp"
android:layout_height="50dp"
android:background="#ff669d" />
</LinearLayout>
加上之後,界面仍然是沉浸式的,狀態欄區域也不再重疊,就像自動補上了 padding:

但問題來了:狀態欄顯示的是最外層 LinearLayout 的白色背景,綠色 TextView 與粉色按鈕整體被往下推。這顯然不是我們要的——我們希望狀態欄的顏色與頂部元件一致。
<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:background="#ffffff"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#009959"
android:clipToPadding="true"
android:fitsSystemWindows="true"
android:text="你好,請問你有男朋友嗎?" />
<Button
android:layout_width="100dp"
android:layout_height="50dp"
android:background="#ff669d" />
</LinearLayout>
把這兩行屬性從外層容器挪到綠色 TextView 身上,就得到我們想要的結果:狀態欄變成與頂部元件相同的綠色,而文字內容自動下移、不再被遮壓。
fitsSystemWindows="true" 的行為是「把系統 UI 的尺寸當成 padding 套在自己身上」。放在外層,被推開的是整個版面;放在頂部元件,被推開的只是那個元件自己的內容,背景色則自然填滿狀態欄區域。這就是兩種寫法視覺差異的來源。
若要進一步做到 Android 5.0 之後那種「狀態欄半透明並帶有漸層」的效果,改用 setStatusBarColor() 搭配 Window.setStatusBarColor() 會更直接;而 API 21 以後也提供了 SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN 等更精細的 flag 組合。這裡的 addFlags 寫法勝在直觀、相容性廣,適合快速把舊專案改造成沉浸式。
用 Android Studio 讀懂 Android 5.x 源碼:從 idegen 到索引完成的完整實踐
做整機開發,最磨人的往往不是寫程式,而是「讀程式」。AOSP 那棵龐大的源碼樹在傳統工具裡幾乎寸步難行,本文要聊的,是如何把這件事交給 Android Studio。 G
Android 應用框架設計:tree 框架的模塊化思路
一個成熟的 Android 應用框架,能夠顯著提升開發效率並降低項目維護成本。本文以 tree 框架為例,梳理其在界面管理、數據存儲、網絡請求、緩存與崩潰處理等方面的設計
GreenDAO 實戰最佳化:從程式碼產生到泛型 Service 封裝
GreenDAO 是一套協助 Android 開發者把 Java 物件快速映射到 SQLite 資料表的 ORM 方案。透過簡潔的物件導向 API,就能完成物件的新增、
Android 三種對話框寫法總結:AlertDialog、Dialog 與 Activity 樣式視窗
專案裡對話框的需求大致分三類:單純的確認提示、需要自訂版面的進度提示、以及需要輸入資料的表單。這三種對應三種不同的實作方式,本篇逐一整理。 一、AlertDialog: