日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

华为底部虚拟导航栏挡住布局

發(fā)布時(shí)間:2023/12/31 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 华为底部虚拟导航栏挡住布局 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

問(wèn)題:在實(shí)現(xiàn)ViewPager+Fragment+側(cè)滑欄的界面時(shí),華為搭載Android5.0以上操作系統(tǒng)的手機(jī)出現(xiàn)底部虛擬導(dǎo)航欄擋住布局。如下圖所示:

問(wèn)題解決后:

嘗試

在實(shí)現(xiàn)這個(gè)功能的時(shí)候,我發(fā)現(xiàn)底部虛擬導(dǎo)航欄遮蓋布局不同的情況對(duì)應(yīng)不同的解決方法。當(dāng)沒(méi)有側(cè)滑功能的時(shí)候,主要有一下兩種:

1. OnCreate()方法中不能出現(xiàn)下邊的代碼:

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

?就是設(shè)置導(dǎo)航欄半透明,這會(huì)使布局向上向下擴(kuò)展至整個(gè)屏幕,導(dǎo)航欄則覆蓋在布局上邊,就會(huì)導(dǎo)致導(dǎo)航欄擋住布局。有的說(shuō)法是換成設(shè)置狀態(tài)欄半透明,如下邊的代碼:

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

?這種做法其實(shí)是不好的,屬于傷敵一千自傷八百。因?yàn)?#xff0c;我們?cè)O(shè)置這個(gè)屬性一般是為了實(shí)現(xiàn)沉浸式狀態(tài)欄的,去掉了第一種代碼,就不能實(shí)現(xiàn)了。比如說(shuō)我使用了SystemBarTint第三方框架來(lái)實(shí)現(xiàn)沉浸式狀態(tài)欄。這時(shí)就需要用到方法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對(duì)這個(gè)屬性的解釋:

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".

翻譯:
?布爾內(nèi)部屬性,基于系統(tǒng)窗口(如狀態(tài)欄)來(lái)調(diào)整視圖布局。如果為true,則調(diào)整此視圖的填充,以便為系統(tǒng)窗口留出空間。只有在非嵌入activity中此視圖才會(huì)生效。

?這個(gè)方法就使系統(tǒng)窗口可以自動(dòng)調(diào)整,可以實(shí)現(xiàn)需求。但是如果界面中有側(cè)滑菜單的,并且實(shí)現(xiàn)了頂部導(dǎo)航欄透明,和底部導(dǎo)航欄顏色填充的話,就需要下邊的方法了。

有效方法

在 style.xml 文件中的項(xiàng)目的主題樣式中添加

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

我們看一下,Android官方API對(duì)這個(gè)屬性的解釋:

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".

翻譯:
?標(biāo)志是指示此窗口是否負(fù)責(zé)繪制系統(tǒng)欄的背景。如果真正的窗口不浮,系統(tǒng)欄被畫(huà)在這個(gè)窗口透明背景和相應(yīng)領(lǐng)域內(nèi)statusbarcolor和navigationbarcolor指定的顏色。對(duì)應(yīng)于flag_draws_system_bar_backgrounds。

?可以看出該屬性是負(fù)責(zé)繪制系統(tǒng)欄的背景的,如果真正的窗口被遮蓋了,設(shè)置true,則會(huì)繪制系統(tǒng)欄的背景,使真正的窗口上移,不被遮擋住。

?如果你的項(xiàng)目兼容的最低版本小于21的話 ,會(huì)紅線提示錯(cuò)誤,雖然可以運(yùn)行但是代碼無(wú)效。解決方法是:在提示錯(cuò)誤的代碼上Alt+Enter,會(huì)提示:



?選擇第一個(gè),就會(huì)自動(dòng)生成適配Android 21的values文件夾:values-v21,里邊有包含該屬性的styles.xml文件。之前添加的報(bào)錯(cuò)的屬性就可以刪掉了。當(dāng)然,你也可以自己新建文件夾,自己實(shí)現(xiàn)。如下圖:



如果不知道項(xiàng)目的主題樣式在哪兒,可以用下邊的查找方式:

?打開(kāi)資源配置文件AndroidManifest.xml,跟進(jìn)屬性 Android:theme=”@style/AppTheme”中的style:



?tips:android:windowDrawsSystemBarBackgrounds在Android官方API文檔版本21以上的可以查到,下邊附一個(gè)我使用的文檔的連接:
最新版Android官方API文檔

總結(jié)

以上是生活随笔為你收集整理的华为底部虚拟导航栏挡住布局的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。