解決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)給我錢的啊),不是華為的手機(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留下空間,如果不寫,布局會(huì)出很嚴(yán)重的問(wèn)題,可以自己去試驗(yàn)不寫的后果…
介紹完基本布局,接下來(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);
//下面的代碼可以寫在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 RxJava創(chuàng)建操作符Timer的方法
這篇文章主要為大家詳細(xì)介紹了Android RxJava創(chuàng)建操作符Timer的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
Android解析相同接口返回不同格式j(luò)son數(shù)據(jù)的方法
這篇文章主要介紹了Android解析相同接口返回不同格式j(luò)son數(shù)據(jù)的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
Android中使用TextView實(shí)現(xiàn)文字跑馬燈效果
本文主要介紹了Android中使用TextView實(shí)現(xiàn)文字走馬燈效果的方法解析。具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧2017-04-04
Android仿微信雷達(dá)輻射搜索好友(邏輯清晰實(shí)現(xiàn)簡(jiǎn)單)
仿微信雷達(dá)掃描,仿安卓微信、云播雷達(dá)掃描動(dòng)畫效果點(diǎn)擊中間的黑色圓圈開(kāi)始掃描動(dòng)畫,再次點(diǎn)擊復(fù)位,需要這種效果的朋友可以自己下載看一下2016-02-02
Android開(kāi)發(fā)之Notification手機(jī)狀態(tài)欄通知用法實(shí)例分析
這篇文章主要介紹了Android開(kāi)發(fā)之Notification手機(jī)狀態(tài)欄通知用法,結(jié)合實(shí)例形式分析了Android Notification手機(jī)狀態(tài)欄通知的常見(jiàn)函數(shù)、功能及使用技巧,需要的朋友可以參考下2019-03-03
android實(shí)現(xiàn)RecyclerView列表單選功能
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)RecyclerView列表單選功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07
Android仿微信和QQ多圖合并框架(類似群頭像)的實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于Android仿微信和QQ多圖合并框架的相關(guān)資料,其實(shí)就是我們平時(shí)所見(jiàn)的群聊頭像,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)各位Android開(kāi)發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12
Android MessageQueue消息隊(duì)列主要作用詳解
Android 消息機(jī)制主要指的是 Handler 的運(yùn)行機(jī)制及其所依賴的 MessageQueue 和 Looper 的工作過(guò)程,Handler、MessageQueue、Looper組成一個(gè)相互聯(lián)系的整體。本文先從 MessageQueue 的源碼來(lái)說(shuō)明其實(shí)現(xiàn)原理2023-02-02

