編輯:關於 Android 程式設計
這個需求是因為專案要求才去研究的。身為 Android 新手,我原本完全沒碰過狀態列整合這件事,第一反應当然是上網搜尋。網路資料確實幫了大忙,但對我這種入門者來說仍不夠細緻,很多關鍵環節沒人講清楚。
折騰幾輪之後總算把效果做出來了。程式碼算不算漂亮我不敢說,但它確實解決了我遇到的問題,於是記錄下來,也許能幫到同樣卡住的人。
先說前提:狀態列整合是 Android 4.4 以後才支援的能力。
成品如下:


activity_main.xml 的結構很單純,用 include 引入標題列再放內容:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/title_layout" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:text="圖片" />
</LinearLayout>
title_layout.xml 是標題列本體,注意根佈局的兩個關鍵屬性:
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="#1f9ff9"
android:clipToPadding="true"
android:fitsSystemWindows="true">
<RelativeLayout
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:background="@android:color/transparent">
<ImageView
android:id="@+id/title_iv_back"
android:layout_width="8dp"
android:layout_height="14dp"
android:layout_centerInParent="true"
android:src="@drawable/back" />
</RelativeLayout>
<TextView
android:id="@+id/title_tv_name"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="@android:color/transparent"
android:ellipsize="end"
android:gravity="center"
android:maxWidth="240dp"
android:singleLine="true"
android:text="標題"
android:textColor="@android:color/white"
android:textSize="18sp" />
</RelativeLayout>
MainActivity 的做法是先把狀態列設為透明,再把標題列的高度補上狀態列的高度:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout rlTitle = (RelativeLayout) findViewById(R.id.title);
// 4.4 以上才支援
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Window win = getWindow();
WindowManager.LayoutParams winParams = win.getAttributes();
final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
winParams.flags |= bits;
win.setAttributes(winParams);
ViewGroup.LayoutParams params = rlTitle.getLayoutParams();
params.height = params.height + getStatusBarHeight();
rlTitle.setLayoutParams(params);
}
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, NewActivity.class));
}
});
}
/** 取得狀態列高度 */
private int getStatusBarHeight() {
int result = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(resourceId);
}
return result;
}
}
activity_new.xml 就是一個鋪滿的 ImageView:
<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:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="@drawable/test" />
</LinearLayout>
NewActivity 只需要一段程式:
public class NewActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Window win = getWindow();
WindowManager.LayoutParams winParams = win.getAttributes();
final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
winParams.flags |= bits;
win.setAttributes(winParams);
}
}
}
第一段,把狀態列設為半透明:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Window win = getWindow();
WindowManager.LayoutParams winParams = win.getAttributes();
final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
winParams.flags |= bits;
win.setAttributes(winParams);
}
第二段,佈局中的兩個屬性:
android:fitsSystemWindows="true" android:clipToPadding="true"
fitsSystemWindows 的官方說明是:一個供系統視窗調整版面用的內部屬性,設為 true 時會為系統視窗(例如狀態列)預留空間。更具體地說,當狀態列呈現透明或半透明(4.4 以上),系統會把該 view 的 paddingTop 設為狀態列高度,讓內容不會被頂到狀態列底下;而在未佔用狀態列的情況(4.4 以下),paddingTop 會是 0,因為本來就不需要留白。
第三段,補回標題列被吃掉的高度:
ViewGroup.LayoutParams params = rlTitle.getLayoutParams(); params.height = params.height + getStatusBarHeight(); rlTitle.setLayoutParams(params);
只寫第一段,畫面變成這樣:

狀態列確實「不見了」,看起來好像整合成功,實際上是整個標題列往上位移、佔據了狀態列的位置,文字和圖示都跟系統資訊重疊在一起。顯然不是我們要的。
加上第二段——請特別注意,這段屬性必須寫在「你希望整合的那個控制項」的根佈局裡。我們想讓狀態列的顏色跟自訂標題列一致,所以就加在 title_layout 的根佈局上。效果改善了一些:

至少文字不再被拉到狀態列裡了,但標題列的高度也一起沒有了。於是我們把第三段補上,在原有高度上再加狀態列高度:

這才是最終想要的樣子。三段程式碼其實分別對應三件事:打通狀態列、讓內容避開系統區域、把被壓縮的高度補回來。
從上面的推演不難發現,圖片整合只需要第一段程式碼,佈局檔完全不動——因為我們本來就希望圖片延伸到狀態列下方,不需要留白。

狀態列一體化之後,最常伴隨出現的麻煩就是軟鍵盤。當頁面同時具備「沉浸式狀態列」與「輸入框」時,鍵盤彈起往往會把輸入欄位遮住,或者讓整個佈局詭異地往上跳。
處理思路大致有三條:在 AndroidManifest 中針對該 Activity 設定 windowSoftInputMode(adjustResize 讓佈局自動縮放、adjustPan 讓系統平移畫面);在根佈局加上 fitsSystemWindows 讓系統正確計算可用區域;若使用 CoordinatorLayout 搭配 NestedScrollView,讓輸入框處於可捲動容器中,鍵盤彈起時自動捲進可視範圍。哪一種有效取決於你的佈局層級,實測是唯一可靠的方法。
微信熱補丁 Tinker 的實踐演進之路
Dev Club 是一個交流移動開發技術、結交同行、拓展人脈的社群,成員均為經過審核的移動開發工程師,每週都會舉辦嘉賓分享與話題討論。本期邀請到騰訊 WXG Androi
Android 自定義控件(二)——onLayout 詳解
先講個笑話:一個乞丐看程序員寫程式,程序員遇到一個怎麼都解決不了的問題,乞丐說「你少打了個分號」,程序員照做,問題果然解決了,於是問:「你怎麼知道?」乞丐笑笑說:「我以前
Android 網路通信入門:下載並顯示網路圖片
本文以最直觀的例子說明 Android 的網路通信:從遠端下載一張圖片,解碼成 Bitmap 後顯示在 ImageView 上。 核心程式碼 public class
ListView 搭配 CheckBox 實作單選與多選效果
先看成果: 單選與多選的差別,主要在於「如何記錄被勾選的項目」。以下直接看程式碼。 一、單選 public class SingleActivity extends