fitsSystemWindows 介紹
根據(jù)官方文檔,如果某個View 的fitsSystemWindows 設(shè)為true,那么該View的padding屬性將由系統(tǒng)設(shè)置,用戶在布局文件中設(shè)置的
padding會被忽略。系統(tǒng)會為該View設(shè)置一個paddingTop,值為statusbar的高度。fitsSystemWindows默認(rèn)為false。
重要說明:
- 只有將statusbar設(shè)為透明,或者界面設(shè)為全屏顯示(設(shè)置View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN flag)時,fitsSystemWindows才會起作用。不然statusbar的空間輪不到用戶處理,這時會由ContentView的父控件處理,如果用HierarchyView 工具查看,將會看到,ContentView的父控件的paddingTop將會被設(shè)置。
- 如果多個view同時設(shè)置了fitsSystemWindows,只有第一個會起作用。這是一般情況,后面會介紹特殊情況。
fitsSystemWindows屬性的個性化
第一次接觸fitsSystemWindows是在CoordinatorLayout控件。發(fā)現(xiàn)有很多詭異的地方。
- fitsSystemWindows的表現(xiàn)和官方文檔描述的不一樣。
- 有時CoordinatorLayout的子控件也會設(shè)置fitsSystemWindows屬性,而且子控件的fitsSystemWindows也會有作用。
這些令我很困惑,查了些資料之后找到了原因:設(shè)置paddingTop只是fitsSystemWindows屬性的默認(rèn)行為,View可以對fitsSystemWindows
進行個性化。fuccccccccccccccck!!!!!!!!!
CoordinatorLayout對fitsSystemWindows的個性化。API 21 以上可以通過調(diào)用View的setOnApplyWindowInsetsListener(OnApplyWindowInsetsListener)函數(shù),改變fitsSystemWindows的默認(rèn)行為。在OnApplyWindowInsetsListener的onApplyWindowInsets函數(shù),可以決定如何處理statusbar的空間。
重要說明:
- 在API 21以前,好像也可以重寫View的某個函數(shù)達到類似效果。
- 必須將statusbar設(shè)為透明,或者界面設(shè)為全屏顯示setOnApplyWindowInsetsListener才會起作用。這點很容易理解,你都沒有statusbar空間,你個性化個屁啊。
CoordinatorLayout對fitsSystemWindows的個性化,關(guān)鍵代碼:
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();
}
}
總結(jié):CoordinatorLayout對fitsSystemWindows主要做了以下處理。
- 將界面設(shè)為全屏。view.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
- 自己繪制statusbar背景。setStatusBarBackground函數(shù)可以設(shè)置statusbar背景?;蛘咴诓季治募型ㄟ^app:statusBarBackground設(shè)置。
- 如果CoordinatorLayout的子View沒有設(shè)置fitsSystemWindows,在layout時將子Viwe向下偏移statusbar的高度,用來顯示CoordinatorLayout繪制的statusbar。如果子view設(shè)置了fitsSystemWindows,子View會覆蓋CoordinatorLayout的statusbar。setStatusBarBackground設(shè)置的狀態(tài)欄
將被覆蓋,不再起作用。具體邏輯可參考CoordinatorLayout的layoutChild 函數(shù)。 - 調(diào)用dispatchApplyWindowInsets,讓子view的behavior或子view接著處理fitsSystemWindows屬性。CoordinatorLayout的很多常用的子view如AppBarLayout也對fitsSystemWindows進行了個性化處理。