編輯:關於 Android 程式設計
沉浸式狀態列並不新鮮,iOS 很早就有,Android 5.0 起也支援替狀態列上色。但由於一段時間內主流版本仍停留在 4.4,網路上常見的相容做法都是依賴「把狀態列變透明再上色」這個思路,核心程式碼如下:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_match_actionbar);
// 僅 API 19 以上有效
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
setTranslucentStatus(true);
}
// 替狀態列上色
SystemBarTintManager tintManager = new SystemBarTintManager(this);
tintManager.setStatusBarTintEnabled(true);
tintManager.setStatusBarTintResource(R.color.statusbar_bg);
}
@TargetApi(19)
private void setTranslucentStatus(boolean on) {
Window win = getWindow();
WindowManager.LayoutParams winParams = win.getAttributes();
final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
if (on) {
winParams.flags |= bits;
} else {
winParams.flags &= ~bits;
}
win.setAttributes(winParams);
}
再在根版面上補上兩個屬性即可:
android:fitsSystemWindows="true" android:clipToPadding="false"
這套寫法應付一般頁面沒問題,但碰上側滑選單這類設計就會出狀況。
參考效果:

在 4.4 的裝置上套用前述方案後,結果變成這樣:

除了視覺上不協調,還有另一個問題:使用抽屜選單或側滑返回時,狀態列沒有陰影層次。

期望的效果則是狀態列一起跟著滑動:

在版面頂端預留一個佔位 View 當作狀態列。注意此時根版面不能加上 android:fitsSystemWindows="true",否則內容會被系統狀態列遮住、填不進去。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorAccent"
android:orientation="vertical">
<View
android:id="@+id/status_bar"
android:layout_width="match_parent"
android:layout_height="20dp" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
接著在程式碼中依版本決定顯示與否,並設定實際高度:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View statusBar = findViewById(R.id.status_bar);
setContentView(R.layout.activity_test);
// API 19 以上才顯示,否則低版本會多出一塊空白
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
setTranslucentStatus(true);
statusBar.setVisibility(View.VISIBLE);
// 各家機型狀態列高度不同,需動態計算後設定 LayoutParams
} else {
statusBar.setVisibility(View.GONE);
}
}
@TargetApi(19)
private void setTranslucentStatus(boolean on) {
Window win = getWindow();
WindowManager.LayoutParams winParams = win.getAttributes();
final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
if (on) {
winParams.flags |= bits;
} else {
winParams.flags &= ~bits;
}
win.setAttributes(winParams);
}
這樣做同時解決了前述兩個問題:狀態列能自訂內容,也能隨版面一起滑動。
思路一會遇到一個棘手狀況:設定了透明屬性的頁面,聊天輸入框不會被系統輸入法頂上去(打字時看不到輸入框)。而加上 android:fitsSystemWindows="true" 這個屬性後輸入框就正常了,但自肋骨況列又無法填到真正的狀態列位置。

加或不加都有明顯問題,於是有了第二種方案。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="@android:color/holo_green_light"
android:fitsSystemWindows="true"
tools:context="com.saidtx.myapplication.TestActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/edit"
android:background="@android:color/white">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="20dp"
android:text="@string/previews" />
</LinearLayout>
</ScrollView>
<LinearLayout
android:id="@+id/edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@android:color/white">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
關鍵就是這兩個屬性:
android:fitsSystemWindows="true" android:background="@android:color/holo_green_light"
程式碼部分沿用網路上的通用方案即可,差別是不再需要自肋骨況列,也不必手動計算狀態列高度。要注意的是,子版面需各自設定背景色,否則會透出最外層的背景。
最終效果:

兩個思路的取捨在於「控制力」與「系統行為相容性」。思路一完全掌控狀態列外觀,但要自行處理高度計算與輸入法;思路二順著系統的 fitsSystemWindows 機制走,相容性較好,卻犧牲了對狀態列內容的控制。
若專案的 minSdk 已高於 21,建議直接採用 Android 5.0 以後的 Window.setStatusBarColor() 與 SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN,不必再依賴 SystemBarTintManager 這類第三方類別,行為也更可預期。
用 TabLayout 實現京東商品詳情頁效果
早在 2015 年的 Google I/O 大會上,Google 就帶來了更為完整的 Material Design 設計規範,同時推出了全新的 Android Desi
非同步載圖第二彈:修掉崩潰、補上進度,並換用 NumberProgressBar
上一節我們用 AsyncTask 完成了非同步下載圖片,但留下了三個問題:無網路時點擊會崩潰、進度條沒有真的更新、以及只能用系統內建的 ProgressBar。這次逐一解
理解 Android 的 2D/3D 圖形架構
要讀懂 Android 的 EGL 呼叫流程,得先抓住幾個物件的關係:display 對應顯示器,config 是顯卡所支援的組態,接著建立 WindowSurface,
Android 6.0 執行期權限完全指南:動態申請流程與懸浮窗的坑
執行期權限是什麼 Android 6.0 在原本於 AndroidManifest.xml 宣告權限的機制之上,多了一道執行期動態檢測。以下九類敏感權限都必須在執行時向使