解決Android 沉浸式狀態(tài)欄和華為虛擬按鍵沖突問題
對于現(xiàn)在的 App 來說,布局頁面基本都會用到沉浸式狀態(tài)欄,單純的沉浸式狀態(tài)欄很容易解決,但是在華為手機(jī)上存在一個底部虛擬按鍵的問題,會導(dǎo)致頁面底部和頂部出現(xiàn)很大的問題,比如頁面底部導(dǎo)航欄被按鍵覆蓋,導(dǎo)致底部無法操作,頂部狀態(tài)欄布局被撐的很高,丑的不忍直視,這里就將兩者的沖突問題一并解決!先看下實現(xiàn)的效果圖:
這是我自己的手機(jī),OnePlus 3T 7.1.1版本(免費(fèi)廣告,沒給我錢的?。?,不是華為的手機(jī),但是有個虛擬按鍵可以設(shè)置,可以看到底部導(dǎo)航欄沒有問題,頂部狀態(tài)欄也成功實現(xiàn),效果圖看完,下面直接飆車了:
主頁面布局:
<?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 直接引入,由于這個不是本篇文章的重點(diǎn)部分,此處不作詳細(xì)介紹,想看具體實現(xiàn)可以看我的源代碼,頂部是一個自定義的 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這個屬性,主要是通過調(diào)整當(dāng)前設(shè)置這個屬性的view的padding去為我們的status_bar留下空間,如果不寫,布局會出很嚴(yán)重的問題,可以自己去試驗不寫的后果…
介紹完基本布局,接下來是如何設(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 個文件夾,分別對應(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>
注意了,接下來就是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);
這句代碼千!萬!不!要!加?。?!加了就會起沖突!到這里,沉浸式狀態(tài)欄和底部虛擬按鍵的沖突問題就得到解決了
最后,貼上我手機(jī)三個手機(jī)的沉浸式狀態(tài)欄截圖,4.4的手機(jī)沒有,(其實是懶得開模擬器…)抱歉不能貼上,這里分別是:
Android 4.2.2,無沉浸式狀態(tài):
Android 5.1:
Android 7.1.1,底部帶虛擬按鍵,也就是我們本篇文章的主題:
以上所述是小編給大家介紹的解決Android 沉浸式狀態(tài)欄和華為虛擬按鍵沖突問題,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Android Flutter實戰(zhàn)之為照片添加顏色濾鏡
這篇文章我們將利用TweenAnimationBuilder來實現(xiàn)一個圖片調(diào)色的過渡動畫,從而實現(xiàn)為照片添加顏色濾鏡的效果,感興趣的可以了解一下2022-12-12Android中fragment嵌套fragment問題解決方法
這篇文章主要介紹了Android中fragment嵌套fragment問題解決方法,本文給出兩個解決方法,需要的朋友可以參考下2015-06-06Android中用Builder模式自定義Dialog的方法
在任何軟件操作系統(tǒng)中,Dialog即對話框都是一種重要的交互模式與信息載體,而Android系統(tǒng)本身的Dialog擁有固定的樣式,并且在5.0后采用Material Design設(shè)計風(fēng)格的Dialog美觀大氣。這篇文章將詳細(xì)介紹Android中用Builder模式自定義Dialog的方法,有需要的可以參考借鑒。2016-10-10Android實現(xiàn)錄音監(jiān)聽動畫的示例代碼
在很多app種內(nèi)置了語音助手,也存在各種動畫,這篇文章主要為大家詳細(xì)介紹了Android實現(xiàn)錄音監(jiān)聽動畫的示例代碼,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-12-12Android性能優(yōu)化之捕獲java crash示例解析
這篇文章主要介紹了Android性能優(yōu)化之捕獲java crash示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09flutter仿微信底部圖標(biāo)漸變功能的實現(xiàn)代碼
這篇文章主要介紹了flutter仿微信底部圖標(biāo)漸變功能,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04