解決Android 沉浸式狀態(tài)欄和華為虛擬按鍵沖突問(wèn)題
對(duì)于現(xiàn)在的 App 來(lái)說(shuō),布局頁(yè)面基本都會(huì)用到沉浸式狀態(tài)欄,單純的沉浸式狀態(tài)欄很容易解決,但是在華為手機(jī)上存在一個(gè)底部虛擬按鍵的問(wèn)題,會(huì)導(dǎo)致頁(yè)面底部和頂部出現(xiàn)很大的問(wèn)題,比如頁(yè)面底部導(dǎo)航欄被按鍵覆蓋,導(dǎo)致底部無(wú)法操作,頂部狀態(tài)欄布局被撐的很高,丑的不忍直視,這里就將兩者的沖突問(wèn)題一并解決!先看下實(shí)現(xiàn)的效果圖:

這是我自己的手機(jī),OnePlus 3T 7.1.1版本(免費(fèi)廣告,沒(méi)給我錢的?。皇侨A為的手機(jī),但是有個(gè)虛擬按鍵可以設(shè)置,可以看到底部導(dǎo)航欄沒(méi)有問(wèn)題,頂部狀態(tài)欄也成功實(shí)現(xiàn),效果圖看完,下面直接飆車了:
主頁(yè)面布局:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.statusbarvirtualkey.statusbarvirtualkey.MainActivity"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <include android:id="@+id/home_title" layout="@layout/layout_home_title" /> <FrameLayout android:id="@+id/home_container" android:layout_width="match_parent" android:layout_height="0dp" android:layout_alignParentTop="true" android:layout_marginTop="70dp" android:layout_above="@+id/line_bottom" android:layout_weight="1" > </FrameLayout> <View android:id="@+id/line_bottom" android:layout_width="match_parent" android:layout_height="0.5dp" android:alpha="0.6" android:layout_above="@+id/bottom_navigation_view" android:background="@android:color/darker_gray" /> <android.support.design.widget.BottomNavigationView android:id="@+id/bottom_navigation_view" android:layout_width="match_parent" android:layout_height="50dp" android:layout_alignParentBottom="true" app:itemBackground="@android:color/white" app:elevation="2dp" app:itemTextColor="@color/selector_item_color" app:itemIconTint="@color/selector_item_color" app:menu="@menu/navigation_menu" > </android.support.design.widget.BottomNavigationView> </RelativeLayout> </RelativeLayout>
底部導(dǎo)航欄用到的是 BottomNavigationView,AndroidStudio 直接引入,由于這個(gè)不是本篇文章的重點(diǎn)部分,此處不作詳細(xì)介紹,想看具體實(shí)現(xiàn)可以看我的源代碼,頂部是一個(gè)自定義的 ToolBar,代碼如下:
<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/myToolBar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/layer_list_bottom_line" android:padding="@dimen/titleBarPadding" android:elevation="@dimen/primary_shadow_height" android:clipToPadding="true" android:fitsSystemWindows="true" app:popupTheme="@style/ThemeOverlay.AppCompat.Light"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/home_left" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:drawableLeft="@mipmap/ind_icon_fan" android:drawablePadding="5dp" android:padding="@dimen/dp_4" /> <TextView android:id="@+id/home_right" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentEnd="true" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:ellipsize="end" android:singleLine="true" android:maxEms="5" android:textColor="@android:color/black" android:textSize="7.0sp" android:typeface="monospace" /> <TextView android:id="@+id/toolbar_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:singleLine="true" android:text="@string/app_name" android:textColor="@android:color/white" android:textSize="20sp" /> </RelativeLayout> </android.support.v7.widget.Toolbar>
需要注意的是Android:fitsSystemWindows這個(gè)屬性,主要是通過(guò)調(diào)整當(dāng)前設(shè)置這個(gè)屬性的view的padding去為我們的status_bar留下空間,如果不寫(xiě),布局會(huì)出很嚴(yán)重的問(wèn)題,可以自己去試驗(yàn)不寫(xiě)的后果…
介紹完基本布局,接下來(lái)是如何設(shè)置沉浸式狀態(tài)欄呢,在 AndroidManifest 布局中,設(shè)置主題:
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>
這里需要注意的是,需要設(shè)置 3 個(gè)文件夾,分別對(duì)應(yīng)的 android 版本不一致,values,values-v19,values-v21,values目錄下設(shè)置:
<style name="AppTheme" parent="@style/BaseAppTheme"> </style> <style name="BaseAppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style>
values-v19:
<style name="AppTheme" parent="@style/BaseAppTheme"> <item name="android:windowTranslucentStatus">true</item> <item name="android:windowTranslucentNavigation">false</item> </style>
values-v21:
<style name="AppTheme" parent="BaseAppTheme"> <item name="android:statusBarColor">@color/colorPrimary</item> <item name="android:windowDrawsSystemBarBackgrounds">true</item> </style>
注意了,接下來(lái)就是Activity里面的重點(diǎn)操作了,這里介紹只貼重點(diǎn)代碼,onCreate方法:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar_title = (TextView) findViewById(R.id.toolbar_title);
//下面的代碼可以寫(xiě)在BaseActivity里面
highApiEffects();
mToolBar = (Toolbar) getWindow().findViewById(R.id.home_title);
setSupportActionBar(mToolBar);
}
@TargetApi(Build.VERSION_CODES.KITKAT)
private void highApiEffects() {
getWindow().getDecorView().setFitsSystemWindows(true);
//透明狀態(tài)欄 @頂部
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//透明導(dǎo)航欄 @底部 這一句不要加,目的是防止沉浸式狀態(tài)欄和部分底部自帶虛擬按鍵的手機(jī)(比如華為)發(fā)生沖突,注釋掉就好了
// getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
高能預(yù)警?。?!這里一定要注意,getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);這句代碼千!萬(wàn)!不!要!加?。?!加了就會(huì)起沖突!到這里,沉浸式狀態(tài)欄和底部虛擬按鍵的沖突問(wèn)題就得到解決了
最后,貼上我手機(jī)三個(gè)手機(jī)的沉浸式狀態(tài)欄截圖,4.4的手機(jī)沒(méi)有,(其實(shí)是懶得開(kāi)模擬器…)抱歉不能貼上,這里分別是:
Android 4.2.2,無(wú)沉浸式狀態(tài):

Android 5.1:

Android 7.1.1,底部帶虛擬按鍵,也就是我們本篇文章的主題:

以上所述是小編給大家介紹的解決Android 沉浸式狀態(tài)欄和華為虛擬按鍵沖突問(wèn)題,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- Android Studio 全屏沉浸式透明狀態(tài)欄效果的實(shí)現(xiàn)
- Android實(shí)現(xiàn)沉浸式狀態(tài)欄功能
- Android編程中沉浸式狀態(tài)欄的三種實(shí)現(xiàn)方式詳解
- Android沉浸式狀態(tài)欄 + actionBar漸變 + scrollView頂部伸縮效果
- Android沉浸式狀態(tài)欄的實(shí)現(xiàn)代碼
- Android沉浸式狀態(tài)欄設(shè)計(jì)的實(shí)例代碼
- Android 沉浸式狀態(tài)欄與隱藏導(dǎo)航欄實(shí)例詳解
- Android 詳解沉浸式狀態(tài)欄的實(shí)現(xiàn)流程
相關(guān)文章
Android Flutter實(shí)戰(zhàn)之為照片添加顏色濾鏡
這篇文章我們將利用TweenAnimationBuilder來(lái)實(shí)現(xiàn)一個(gè)圖片調(diào)色的過(guò)渡動(dòng)畫(huà),從而實(shí)現(xiàn)為照片添加顏色濾鏡的效果,感興趣的可以了解一下2022-12-12
Android中fragment嵌套fragment問(wèn)題解決方法
這篇文章主要介紹了Android中fragment嵌套fragment問(wèn)題解決方法,本文給出兩個(gè)解決方法,需要的朋友可以參考下2015-06-06
Android中用Builder模式自定義Dialog的方法
在任何軟件操作系統(tǒng)中,Dialog即對(duì)話框都是一種重要的交互模式與信息載體,而Android系統(tǒng)本身的Dialog擁有固定的樣式,并且在5.0后采用Material Design設(shè)計(jì)風(fēng)格的Dialog美觀大氣。這篇文章將詳細(xì)介紹Android中用Builder模式自定義Dialog的方法,有需要的可以參考借鑒。2016-10-10
Android實(shí)現(xiàn)錄音監(jiān)聽(tīng)動(dòng)畫(huà)的示例代碼
在很多app種內(nèi)置了語(yǔ)音助手,也存在各種動(dòng)畫(huà),這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)錄音監(jiān)聽(tīng)動(dòng)畫(huà)的示例代碼,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-12-12
Android性能優(yōu)化之捕獲java crash示例解析
這篇文章主要介紹了Android性能優(yōu)化之捕獲java crash示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
android編程之下拉刷新實(shí)現(xiàn)方法分析
這篇文章主要介紹了android編程之下拉刷新實(shí)現(xiàn)方法,以實(shí)例形式詳細(xì)分析了Android編程中針對(duì)ListView下拉刷新的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11
flutter仿微信底部圖標(biāo)漸變功能的實(shí)現(xiàn)代碼
這篇文章主要介紹了flutter仿微信底部圖標(biāo)漸變功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04
Android ZxingPlus精簡(jiǎn)的二維碼框架示例代碼
下面小編就為大家分享一篇Android ZxingPlus精簡(jiǎn)的二維碼框架示例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01

