android:fitsSystemWindows屬性的用法

來源:互聯網
上載者:User

標籤: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屬性的用法

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.