fitsSystemWindows 介紹
根據官方文檔,如果某個View 的fitsSystemWindows 設為true,那么該View的padding屬性將由系統設置,用戶在布局文件中設置的
padding會被忽略。系統會為該View設置一個paddingTop,值為statusbar的高度。fitsSystemWindows默認為false。
重要說明:
- 只有將statusbar設為透明,或者界面設為全屏顯示(設置View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN flag)時,fitsSystemWindows才會起作用。不然statusbar的空間輪不到用戶處理,這時會由ContentView的父控件處理,如果用HierarchyView 工具查看,將會看到,ContentView的父控件的paddingTop將會被設置。
- 如果多個view同時設置了fitsSystemWindows,只有第一個會起作用。這是一般情況,后面會介紹特殊情況。
fitsSystemWindows屬性的個性化
第一次接觸fitsSystemWindows是在CoordinatorLayout控件。發現有很多詭異的地方。
- fitsSystemWindows的表現和官方文檔描述的不一樣。
- 有時CoordinatorLayout的子控件也會設置fitsSystemWindows屬性,而且子控件的fitsSystemWindows也會有作用。
這些令我很困惑,查了些資料之后找到了原因:設置paddingTop只是fitsSystemWindows屬性的默認行為,View可以對fitsSystemWindows
進行個性化。fuccccccccccccccck!!!!!!!!!
CoordinatorLayout對fitsSystemWindows的個性化。API 21 以上可以通過調用View的setOnApplyWindowInsetsListener(OnApplyWindowInsetsListener)函數,改變fitsSystemWindows的默認行為。在OnApplyWindowInsetsListener的onApplyWindowInsets函數,可以決定如何處理statusbar的空間。
重要說明:
- 在API 21以前,好像也可以重寫View的某個函數達到類似效果。
- 必須將statusbar設為透明,或者界面設為全屏顯示setOnApplyWindowInsetsListener才會起作用。這點很容易理解,你都沒有statusbar空間,你個性化個屁啊。
CoordinatorLayout對fitsSystemWindows的個性化,關鍵代碼:
if (ViewCompat.getFitsSystemWindows(view)) {
// First apply the insets listener
ViewCompat.setOnApplyWindowInsetsListener(view, insetsListener);
// Now set the sys ui flags to enable us to lay out in the window insets
view.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}
final class ApplyInsetsListener implements android.support.v4.view.OnApplyWindowInsetsListener {
@Override
public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) {
setWindowInsets(insets);
return insets.consumeSystemWindowInsets();
}
}
總結:CoordinatorLayout對fitsSystemWindows主要做了以下處理。
- 將界面設為全屏。view.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
- 自己繪制statusbar背景。setStatusBarBackground函數可以設置statusbar背景。或者在布局文件中通過app:statusBarBackground設置。
- 如果CoordinatorLayout的子View沒有設置fitsSystemWindows,在layout時將子Viwe向下偏移statusbar的高度,用來顯示CoordinatorLayout繪制的statusbar。如果子view設置了fitsSystemWindows,子View會覆蓋CoordinatorLayout的statusbar。setStatusBarBackground設置的狀態欄
將被覆蓋,不再起作用。具體邏輯可參考CoordinatorLayout的layoutChild 函數。 - 調用dispatchApplyWindowInsets,讓子view的behavior或子view接著處理fitsSystemWindows屬性。CoordinatorLayout的很多常用的子view如AppBarLayout也對fitsSystemWindows進行了個性化處理。