华为底部虚拟导航栏挡住布局
問題:在實現ViewPager+Fragment+側滑欄的界面時,華為搭載Android5.0以上操作系統的手機出現底部虛擬導航欄擋住布局。如下圖所示:
問題解決后:
嘗試
在實現這個功能的時候,我發現底部虛擬導航欄遮蓋布局不同的情況對應不同的解決方法。當沒有側滑功能的時候,主要有一下兩種:
1. OnCreate()方法中不能出現下邊的代碼:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);?就是設置導航欄半透明,這會使布局向上向下擴展至整個屏幕,導航欄則覆蓋在布局上邊,就會導致導航欄擋住布局。有的說法是換成設置狀態欄半透明,如下邊的代碼:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);?這種做法其實是不好的,屬于傷敵一千自傷八百。因為,我們設置這個屬性一般是為了實現沉浸式狀態欄的,去掉了第一種代碼,就不能實現了。比如說我使用了SystemBarTint第三方框架來實現沉浸式狀態欄。這時就需要用到方法2了。
2. 在布局的根布局中添加android:fitsSystemWindows=”true”
比如:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:fitsSystemWindows="trueandroid:orientation="vertical"><View android:layout_width="match_parent"android:layout_height="@dimen/theme_divide_height"android:background="#3D81D6"/></LinearLayout>我們看一下,Android官方API對這個屬性的解釋:
Boolean internal attribute to adjust view layout based on system windows such as the status bar. 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.May be a boolean value, such as "true" or "false".翻譯:
?布爾內部屬性,基于系統窗口(如狀態欄)來調整視圖布局。如果為true,則調整此視圖的填充,以便為系統窗口留出空間。只有在非嵌入activity中此視圖才會生效。
?這個方法就使系統窗口可以自動調整,可以實現需求。但是如果界面中有側滑菜單的,并且實現了頂部導航欄透明,和底部導航欄顏色填充的話,就需要下邊的方法了。
有效方法
在 style.xml 文件中的項目的主題樣式中添加
<item name="android:windowDrawsSystemBarBackgrounds">false</item>我們看一下,Android官方API對這個屬性的解釋:
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 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.May be a boolean value, such as "true" or "false".翻譯:
?標志是指示此窗口是否負責繪制系統欄的背景。如果真正的窗口不浮,系統欄被畫在這個窗口透明背景和相應領域內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文檔
總結
以上是生活随笔為你收集整理的华为底部虚拟导航栏挡住布局的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微信提现手续费怎么算
- 下一篇: 股票ddx指标是什么意思