編輯:關於 Android 程式設計
伴隨 Android 5.0 發布的 Material Design,讓 Android 應用告別了以往粗糙的視覺風格,也讓越來越多的 App 開始真正遵循 Material 設計規範,而不是拿著 iOS 的設計稿做 Android 開發。本文聚焦其中的沉浸式狀態欄,講解如何兼容到 4.4,以及實踐中的一些細節。
4.4 之前狀態欄一直是黑色的。4.4 引入了 windowTranslucentStatus 特性,讓狀態欄可以自定義顏色:

國內普遍把狀態欄變色稱為「沉浸式狀態欄」,叫的人多了,也就約定俗成。嚴格說這與 Translucent Bars 並非同一概念,知乎上關於「為什麼國內用戶常把透明欄稱作沉浸式頂欄」的討論可以參考。
4.4 及以上都可以實現沉浸式效果,5.0 及以上更簡單——直接在主題裡設 colorPrimaryDark,或調用 Window 的 setStatusBarColor(int)。真正的麻煩在於兼容到 4.4,以及以下兩個衍生場景:
1. 圖片作為背景的頁面,如何讓狀態欄透明或半透明?

2. 使用 DrawerLayout 時,如何保證主界面沉浸的同時,抽屜視圖也能延伸到狀態欄?

思路是「借屍還魂」:先把系統狀態欄設為透明,再在頂部覆蓋一個與狀態欄等高的色塊,最後讓根布局延伸到狀態欄區域。這樣視覺上色的是狀態欄,實際上是那個色塊。
public static void setColor(Activity activity, int color) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
View statusView = createStatusView(activity, color);
ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView();
decorView.addView(statusView);
ViewGroup rootView = (ViewGroup) ((ViewGroup) activity
.findViewById(android.R.id.content)).getChildAt(0);
rootView.setFitsSystemWindows(true);
rootView.setClipToPadding(true);
}
}
private static View createStatusView(Activity activity, int color) {
int resourceId = activity.getResources()
.getIdentifier("status_bar_height", "dimen", "android");
int statusBarHeight = activity.getResources().getDimensionPixelSize(resourceId);
View statusView = new View(activity);
statusView.setLayoutParams(new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, statusBarHeight));
statusView.setBackgroundColor(color);
return statusView;
}
在 setContentView() 之後調用 setColor() 即可。這裡有個關鍵點:setFitsSystemWindows(true) 讓根布局自動加上狀態欄高度的 padding,內容才不會被狀態欄遮擋。
這種情況更簡單,去掉色塊,只留透明 flag 和 FitsSystemWindows:
public static void setTranslucent(Activity activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
ViewGroup rootView = (ViewGroup) ((ViewGroup) activity
.findViewById(android.R.id.content)).getChildAt(0);
rootView.setFitsSystemWindows(true);
rootView.setClipToPadding(true);
}
}
前提是把背景圖設在根布局上,這樣圖片才會自然填充到狀態欄下方。
注意點:使用 DrawerLayout 時,不能再對根布局(即 DrawerLayout 本身)設置,而要針對它的內容布局——抽屜之外的那一層。典型結構如下:
<android.support.v4.widget.DrawerLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" />
</FrameLayout>
<NavigationView ... />
</android.support.v4.widget.DrawerLayout>
要操作的是 FrameLayout;同時記得把抽屜布局(NavigationView)的 fitsSystemWindows 設為 false。
狀態欄變色:
public static void setColorForDrawerLayout(Activity activity, DrawerLayout drawerLayout, int color) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
View statusBarView = createStatusBarView(activity, color);
ViewGroup contentLayout = (ViewGroup) drawerLayout.getChildAt(0);
contentLayout.addView(statusBarView, 0);
if (!(contentLayout instanceof LinearLayout) && contentLayout.getChildAt(1) != null) {
contentLayout.getChildAt(1).setPadding(0, getStatusBarHeight(activity), 0, 0);
}
ViewGroup drawer = (ViewGroup) drawerLayout.getChildAt(1);
drawerLayout.setFitsSystemWindows(false);
contentLayout.setFitsSystemWindows(false);
contentLayout.setClipToPadding(true);
drawer.setFitsSystemWindows(false);
}
}
這裡最容易踩的坑是:DrawerLayout 只能有兩個直接子布局,第一個是內容布局,第二個是抽屜布局。如果內容布局的根不是 LinearLayout,必須手動給它的子布局設置 paddingTop,否則色塊會被內容蓋住,布局內容也會延伸到狀態欄下方:

坦率說,這部分方案並不算優雅,如果有更好的做法歡迎交流。
狀態欄透明:
public static void setTranslucentForDrawerLayout(Activity activity, DrawerLayout drawerLayout) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
ViewGroup contentLayout = (ViewGroup) drawerLayout.getChildAt(0);
contentLayout.setFitsSystemWindows(true);
contentLayout.setClipToPadding(true);
ViewGroup vg = (ViewGroup) drawerLayout.getChildAt(1);
vg.setFitsSystemWindows(false);
drawerLayout.setFitsSystemWindows(false);
}
}
把上述方法整理成工具類 StatusBarUtils 之後,推薦這樣使用:在 BaseActivity 中重寫 setContentView(),統一設置主題色,因為絕大多數頁面的狀態欄顏色是一致的。
public class BaseActivity extends AppCompatActivity {
@Override
public void setContentView(int layoutResID) {
super.setContentView(layoutResID);
setStatusBar();
}
protected void setStatusBar() {
StatusBarUtils.setColor(this, getResources().getColor(R.color.colorPrimary));
}
}
子類需要特殊處理時,重寫 setStatusBar() 即可:
public class ImageStatusBarActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_status_bar);
}
@Override
protected void setStatusBar() {
StatusBarUtils.setTranslucent(this);
}
}
使用 DrawerLayout 版本的方法時要特別小心:由於調用時機在 setContentView() 之後立即執行,傳入的 DrawerLayout 必須直接通過 findViewById() 取得。如果先保存 Activity 引用、稍後再 findViewById,就會拿到空對象。
StatusBarUtils.setColorForDrawerLayout(this,
(DrawerLayout) findViewById(R.id.drawer_layout),
getResources().getColor(R.color.colorPrimary));
第一,5.0 及以上其實不需要這套「假色塊」方案,直接設 statusBarColor 就好,工具類裡最好做版本判斷分流,避免多一層無意味的 View。
第二,FLAG_TRANSLUCENT_STATUS 與 FLAG_LAYOUT_NO_LIMITS 等 flag 一起使用時容易互相干擾,如果出現鍵盤彈起後布局異常,可以檢查一下 flag 組合。
第三,Android 15 起強制 edge-to-edge 顯示,系統欄會變成浮動層,這套依賴 status_bar_height 的手工方案需要重新評估,建議新項目直接採用 WindowInsets API 來處理。
源碼與 Demo 已整理上傳至 GitHub(StatusBarDemo、StatusBarUtils.java),效果截圖見前文。文中若有疏漏,歡迎指出。
PXbox安卓模擬器多開教學 靠沙盤實現雙開
在電腦上玩手機遊戲往往離不開安卓模擬器,而出於工作或多帳號需求,聊天類應用常需要多開。下面小編就講解如何用沙盤實現PKBox安卓模擬器的多開。 請先將PKXbox安卓模擬
Android RecyclerView 入門:從 ViewHolder 到自訂點擊事件
RecyclerView 到底是什麼 把它理解成 ListView 的進化版最省事。ListView 之所以卡,是因為 item 大量重建;解法是 ViewHolder—
Android 自訂檢視:在圖片上自由塗鴉與橡皮擦功能實作
隨著 Android 生態日益成熟,你想要的大多數檢視在 GitHub 上幾乎都找得到。不過對喜歡折騰的人來說,偶爾自己動手做一個會更有成就感。之前提過想研究圖片相關的處
Android 5.0 常用顏色屬性說明
在 Eclipse 時代,開發者較少在 style 中統一配置全局顏色。Material Design 出現後,通過主題配置可以快速提升界面一致性。Android 5.0