Android Studio 全屏沉浸式透明狀態(tài)欄效果的實(shí)現(xiàn)
如何實(shí)現(xiàn)?1.)首先實(shí)現(xiàn)全屏
第一種:繼承主題特定主題
在Android API 19以上可以使用****.TranslucentDecor***有關(guān)的主題,自帶相應(yīng)半透明效果,Theme.Holo.NoActionBar.TranslucentDecor和Theme.Holo.Light.NoActionBar.TranslucentDecor兩種主題為新增加的,所以要新建values-v19文件夾并創(chuàng)建styles文件添加如下代碼
<style name="AppBaseTheme" parent="android:Theme.Holo.Light.NoActionBar.TranslucentDecor"> <!-- Customize your theme here. --> </style>
第二種:在activity中采用代碼的方式
Android 4.4以上可以添加如下代碼
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { //透明狀態(tài)欄 window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); //透明導(dǎo)航欄 window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); }
Android 5.0 以上也可以使用下面的代碼實(shí)現(xiàn)全屏
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_STABLE); window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); }
2.)解決狀態(tài)欄占位問(wèn)題
第一種:主題添加如下設(shè)置
<item name="android:fitsSystemWindows">true</item>
第二種:activity layout根目錄添加下面代碼
android:fitsSystemWindows="true"
第三種:通過(guò)Java代碼設(shè)置
rootview.setFitsSystemWindows(true);
3.)狀態(tài)欄導(dǎo)航欄設(shè)置背景色
4.4以上的可以采用修改contentView的背景色,或者動(dòng)態(tài)添加一個(gè)view到contentView上
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { //透明狀態(tài)欄 window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); //透明導(dǎo)航欄 window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); //設(shè)置contentview為fitsSystemWindows ViewGroup contentView = (ViewGroup) findViewById(android.R.id.content); View childAt = contentView.getChildAt(0); if (childAt != null) { childAt.setFitsSystemWindows(true); } //給statusbar著色 View view = new View(this); view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight(this))); view.setBackgroundColor(color); contentView.addView(view); }
動(dòng)態(tài)獲取StatusBarHeight函數(shù)如下
/** * 獲取狀態(tài)欄高度 * * @param context context * @return 狀態(tài)欄高度 */ private static int getStatusBarHeight(Context context) { // 獲得狀態(tài)欄高度 int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android"); return context.getResources().getDimensionPixelSize(resourceId); }
動(dòng)態(tài)獲取NavigationBarHeight函數(shù)如下
/** * 獲取導(dǎo)航欄高度 * * @param context context * @return 導(dǎo)航欄高度 */ public static int getNavigationBarHeight(Context context) { int resourceId = context.getResources().getIdentifier("navigation_bar_height", "dimen", "android"); return context.getResources().getDimensionPixelSize(resourceId); }
4.)貼出整體java代碼實(shí)現(xiàn)方式
private void initWindows() { Window window = getWindow(); int color = getResources().getColor(R.color.wechatBgColor); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_STABLE); window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); //設(shè)置狀態(tài)欄顏色 window.setStatusBarColor(color); //設(shè)置導(dǎo)航欄顏色 window.setNavigationBarColor(getResources().getColor(R.color.footerBgColor)); ViewGroup contentView = ((ViewGroup) findViewById(android.R.id.content)); View childAt = contentView.getChildAt(0); if (childAt != null) { childAt.setFitsSystemWindows(true); } } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { //透明狀態(tài)欄 window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); //透明導(dǎo)航欄 window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); //設(shè)置contentview為fitsSystemWindows ViewGroup contentView = (ViewGroup) findViewById(android.R.id.content); View childAt = contentView.getChildAt(0); if (childAt != null) { childAt.setFitsSystemWindows(true); } //給statusbar著色 View view = new View(this); view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight(this))); view.setBackgroundColor(color); contentView.addView(view); } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && useStatusBarColor) {//android6.0以后可以對(duì)狀態(tài)欄文字顏色和圖標(biāo)進(jìn)行修改 getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR); } }
到此這篇關(guān)于Android Studio 全屏沉浸式透明狀態(tài)欄效果的文章就介紹到這了,更多相關(guān)Android Studio 全屏沉浸式透明狀態(tài)欄內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guā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)欄和華為虛擬按鍵沖突問(wèn)題
- Android 沉浸式狀態(tài)欄與隱藏導(dǎo)航欄實(shí)例詳解
- Android 詳解沉浸式狀態(tài)欄的實(shí)現(xiàn)流程
相關(guān)文章
Android實(shí)現(xiàn)隨機(jī)圓形云標(biāo)簽效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)隨機(jī)圓形云標(biāo)簽效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05Android學(xué)習(xí)筆記——Menu介紹(二)
這次將繼續(xù)上一篇文章沒(méi)有講完的Menu的學(xué)習(xí),上下文菜單(Context menu)和彈出菜單(Popup menu)2014-10-10Android項(xiàng)目中使用HTTPS配置的步驟詳解
這篇文章主要給大家介紹了關(guān)于Android項(xiàng)目中使用HTTPS配置步驟的相關(guān)資料,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-06-06Android利用CountDownTimer實(shí)現(xiàn)點(diǎn)擊獲取驗(yàn)證碼倒計(jì)時(shí)效果
這篇文章主要為大家詳細(xì)介紹了Android利用CountDownTimer實(shí)現(xiàn)點(diǎn)擊獲取驗(yàn)證碼倒計(jì)時(shí)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03Android 實(shí)現(xiàn)圖片生成卷角和圓角縮略圖的方法
本篇文章主要介紹了Android 實(shí)現(xiàn)圖片生成卷角和圓角縮略圖的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11Android實(shí)現(xiàn)垂直進(jìn)度條VerticalSeekBar
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)垂直進(jìn)度條VerticalSeekBar的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07android中在Activity中響應(yīng)ListView內(nèi)部按鈕的點(diǎn)擊事件的兩種方法
本篇文章主要介紹了android中在Activity中響應(yīng)ListView內(nèi)部按鈕的點(diǎn)擊事件的兩種方法,有需要的可以了解一下。2016-11-11