1. 程式人生 > >android- 虛擬導航欄擋住底部內容佈局

android- 虛擬導航欄擋住底部內容佈局

使用2解決

 

 

 

問題:在實現ViewPager+Fragment+側滑欄的介面時,華為搭載Android5.0以上作業系統的手機出現底部虛擬導航欄擋住佈局。如下圖所示:

這裡寫圖片描述

問題解決後: 
這裡寫圖片描述

嘗試

在實現這個功能的時候,我發現底部虛擬導航欄遮蓋佈局不同的情況對應不同的解決方法。當沒有側滑功能的時候,主要有一下兩種:

1. OnCreate()方法中不能出現下邊的程式碼:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
  • 1

 就是設定導航欄半透明,這會使佈局向上向下擴充套件至整個螢幕,導航欄則覆蓋在佈局上邊,就會導致導航欄擋住佈局。有的說法是換成設定狀態列半透明,如下邊的程式碼:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
  • 1

 這種做法其實是不好的,屬於傷敵一千自傷八百。因為,我們設定這個屬性一般是為了實現沉浸式狀態列的,去掉了第一種程式碼,就不能實現了。比如說我使用了SystemBarTint第三方框架來實現沉浸式狀態列。這時就需要用到方法2了。 
 

2. 在佈局的根佈局中新增android:fitsSystemWindows=”true”

比如:

 
  1. <LinearLayout

  2. xmlns:android="http://schemas.android.com/apk/res/android"

  3. android:layout_width="match_parent"

  4. android:layout_height="match_parent"

  5. android:fitsSystemWindows="true

  6. android:orientation="vertical">

  7.  
  8. <View

  9. android:layout_width="match_parent"

  10. android:layout_height="@dimen/theme_divide_height"

  11. android:background="#3D81D6"/>

  12.  
  13. </LinearLayout>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

我們看一下,Android官方API對這個屬性的解釋:

 
  1. Boolean internal attribute to adjust view layout based on system windows such as the status bar.

  2. If true, adjusts the padding of this view to leave space for the system windows. Will only take effect if this view is in a non-embedded activity.

  3.  
  4. May be a boolean value, such as "true" or "false".

  • 1
  • 2
  • 3
  • 4

翻譯: 
 布林內部屬性,基於系統視窗(如狀態列)來調整檢視佈局。如果為true,則調整此檢視的填充,以便為系統視窗留出空間。只有在非嵌入activity中此檢視才會生效。 

 這個方法就使系統視窗可以自動調整,可以實現需求。但是如果介面中有側滑選單的,並且實現了頂部導航欄透明,和底部導航欄顏色填充的話,就需要下邊的方法了。 
 

有效方法

在 style.xml 檔案中的專案的主題樣式中新增

<item name="android:windowDrawsSystemBarBackgrounds">false</item>
  • 1

我們看一下,Android官方API對這個屬性的解釋:

 
  1. Flag indicating whether this Window is responsible for drawing the background for the system bars. If true and the window is not floating, the system bars are

  2. drawn with a transparent background and the corresponding areas in this window are filled with the colors specified in statusBarColor and navigationBarColor. Corresponds to FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS.

  3.  
  4. May be a boolean value, such as "true" or "false".

  • 1
  • 2
  • 3
  • 4

翻譯: 
 標誌是指示此視窗是否負責繪製系統欄的背景。如果真正的視窗不浮,系統欄被畫在這個視窗透明背景和相應領域內statusbarcolor和navigationbarcolor指定的顏色。對應於flag_draws_system_bar_backgrounds。 

 可以看出該屬性是負責繪製系統欄的背景的,如果真正的視窗被遮蓋了,設定true,則會繪製系統欄的背景,使真正的視窗上移,不被遮擋住。 

 如果你的專案相容的最低版本小於21的話 ,會紅線提示錯誤,雖然可以執行但是程式碼無效。解決方法是:在提示錯誤的程式碼上Alt+Enter,會提示: 

這裡寫圖片描述

 選擇第一個,就會自動生成適配Android 21的values資料夾:values-v21,裡邊有包含該屬性的styles.xml檔案。之前新增的報錯的屬性就可以刪掉了。當然,你也可以自己新建資料夾,自己實現。如下圖: 

這裡寫圖片描述 
 

如果不知道專案的主題樣式在哪兒,可以用下邊的查詢方式:

 開啟資源配置檔案AndroidManifest.xml,跟進屬性 Android:theme=”@style/AppTheme”中的style: 

這裡寫圖片描述 

 tips:android:windowDrawsSystemBarBackgrounds在Android官方API文件版本21以上的可以查到,下邊附一個我使用的文件的連線: 
最新版Android官方API文件 

 好了,到此就結束了。希望能幫到大家。