標籤:The result log net rac usb 技術 附加 height
屬性說明
fitsSystemWindows屬性可以讓view根據系統視窗來調整自己的布局;簡單點說就是我們在設定應用布局時是否考慮系統視窗布局,這裡系統視窗包括系統狀態列、導覽列、IME等,包括一些手機系統帶有的底部虛擬按鍵。
android:fitsSystemWindows=”true” (觸發View的padding屬性來給系統視窗留出空間)
這個屬性可以給任何view設定,只要設定了這個屬性此view的其他所有padding屬性失效,同時該屬性的生效條件是只有在設定了透明狀態列(StatusBar)或者導覽列(NavigationBar)此屬性才會生效。
注意: fitsSystemWindows只作用在Android4.4及以上的系統,因為4.4以下的系統StatusBar沒有透明狀態。
應用情境
在不同Android版本下,App狀態列和不同版本中系統本身的狀態列的適配;
相容帶有底部虛擬按鍵的手機系統。
屬性使用
1、預設效果
先貼一張未對系統狀態列和導覽列做透明設定時測試布局:
2、系統視窗透明後效果
當設定了透明狀態列(StatusBar)和透明導覽列(NavigationBar)時:
透明狀態列代碼設定:
//布局設定<item name="android:windowTranslucentStatus">true</item>//或者代碼設定if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);}
透明導覽列代碼設定:
//布局設定<item name="android:windowTranslucentNavigation">true</item>//或者代碼設定if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);}
如果有以上兩種情況之一,我們的狀態列(StatusBar)或導覽列(NavigationBar)就會變成透明,並且布局會擴充到StatusBar或NavigationBar的位置。
注意:這裡有個關於狀態列和導覽列透明樣式的問題,這個是受Android版本決定,4.4以下無透明效果,4.4~5.0是全透明,5.0以上是半透明。我使用的是5.0以上的版本模擬器進行測試的,所以是半透明。
3、設定fitsSystemWindows屬性後效果
現在就到了我們關鍵的fitsSystemWindows屬性登場了,只要在根布局中加上android:fitsSystemWindows=”true”效果如:
設定了android:fitsSystemWindows=”true”屬性後針對透明的狀態列會自動添加一個值等於狀態列高度的paddingTop;針對透明的系統導覽列會自動添加一個值等於導覽列高度的paddingBottom
貼上布局的代碼:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="45dp" android:background="#66FF4081" android:gravity="center" android:text="App標題列" android:textSize="30sp" /> <TextView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:background="#0CE3EE" android:gravity="center" android:text="App內容部分" android:textSize="30sp"/> <TextView android:layout_width="match_parent" android:layout_height="45dp" android:background="#4188F8" android:gravity="center" android:text="App導覽列" android:textSize="30sp"/></LinearLayout>
追及
附加一個擷取狀態列StatusBar的和一個擷取導覽列NavigationBar高度的java代碼:
//傳回值就是狀態列的高度,得到的值單位pxpublic float getStatusBarHeight() { float result = 0; int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android"); if (resourceId > 0) { result = getResources().getDimension(resourceId); } return result;}
//傳回值就是導覽列的高度,得到的值單位pxpublic float getNavigationBarHeight() { float result = 0; int resourceId = getResources().getIdentifier("navigation_bar_height", "dimen", "android"); if (resourceId > 0) { result = getResources().getDimension(resourceId); } return result;}
android:fitsSystemWindows屬性的用法