項目中咱們有時候都要用的透明狀態欄(這裏也成沉浸式狀態欄),今天介紹一個gyf-dev寫的一個封裝狀態欄開源框架java
效果圖以下:android
從Android4.4開始,才能夠實現狀態欄着色,而且從5.0開始系統更加完善了這一功能。以前寫過關於一篇 關於activity狀態欄的一些總結 有關Activity樣式 、狀態欄透明、屏幕亮度問題應用場景及其總結git
getWindow().setBackgroundDrawable(null); //設置透明狀態欄 ViewGroup contentFrameLayout = (ViewGroup) findViewById(Window.ID_ANDROID_CONTENT); View parentView = contentFrameLayout.getChildAt(0); if (parentView != null && Build.VERSION.SDK_INT >= 14) { parentView.setFitsSystemWindows(true); }
或者也能夠這樣 : app
將佈局延伸到狀態欄來處理,此次咱們使用android:fitsSystemWindows=」true」屬性,不讓佈局延伸到狀態欄,這時狀態欄就是透明的,而後添加一個和狀態欄高、寬相同的指定顏色View來覆蓋被透明化的狀態欄。咱們一步步來實現。框架
ImmersionBar.with(this) .transparentStatusBar() //透明狀態欄,不寫默認透明色 .transparentNavigationBar() //透明導航欄,不寫默認黑色(設置此方法,fullScreen()方法自動爲true) .transparentBar() //透明狀態欄和導航欄,不寫默認狀態欄爲透明色,導航欄爲黑色(設置此方法,fullScreen()方法自動爲true) .statusBarColor(R.color.colorPrimary) //狀態欄顏色,不寫默認透明色 .navigationBarColor(R.color.colorPrimary) //導航欄顏色,不寫默認黑色 .barColor(R.color.colorPrimary) //同時自定義狀態欄和導航欄顏色,不寫默認狀態欄爲透明色,導航欄爲黑色 .statusBarAlpha(0.3f) //狀態欄透明度,不寫默認0.0f .navigationBarAlpha(0.4f) //導航欄透明度,不寫默認0.0F .barAlpha(0.3f) //狀態欄和導航欄透明度,不寫默認0.0f .statusBarDarkFont(true) //狀態欄字體是深色,不寫默認爲亮色 .flymeOSStatusBarFontColor(R.color.btn3) //修改flyme OS狀態欄字體顏色 .fullScreen(true) //有導航欄的狀況下,activity全屏顯示,也就是activity最下面被導航欄覆蓋,不寫默認非全屏 .hideBar(BarHide.FLAG_HIDE_BAR) //隱藏狀態欄或導航欄或二者,不寫默認不隱藏 .addViewSupportTransformColor(toolbar) //設置支持view變色,能夠添加多個view,不指定顏色,默認和狀態欄同色,還有兩個重載方法 .titleBar(view) //解決狀態欄和佈局重疊問題,任選其一 .statusBarView(view) //解決狀態欄和佈局重疊問題,任選其一 .fitsSystemWindows(true) //解決狀態欄和佈局重疊問題,任選其一,默認爲false,當爲true時必定要指定statusBarColor(),否則狀態欄爲透明色 .supportActionBar(true) //支持ActionBar使用 .statusBarColorTransform(R.color.orange) //狀態欄變色後的顏色 .navigationBarColorTransform(R.color.orange) //導航欄變色後的顏色 .barColorTransform(R.color.orange) //狀態欄和導航欄變色後的顏色 .removeSupportView(toolbar) //移除指定view支持 .removeSupportAllView() //移除所有view支持 .navigationBarEnable(true) //是否能夠修改導航欄顏色,默認爲true .navigationBarWithKitkatEnable(true) //是否能夠修改安卓4.4和emui3.1手機導航欄顏色,默認爲true .fixMarginAtBottom(true) //當xml裏使用android:fitsSystemWindows="true"屬性時,解決4.4和emui3.1手機底部有時會出現多餘空白的問題,默認爲false,非必須 .addTag("tag") //給以上設置的參數打標記 .getTag("tag") //根據tag得到沉浸式參數 .reset() //重置因此沉浸式參數 .keyboardEnable(true) //解決軟鍵盤與底部輸入框衝突問題,默認爲false,還有一個重載方法,能夠指定軟鍵盤mode .init(); //必須調用方可沉浸式
ImmersionBar.with(this).init();
在activity的onDestroy方法中執行ide
ImmersionBar.with(this).destroy(); //不調用該方法,若是界面bar發生改變,在不關閉app的狀況下,退出此界面再進入將記憶最後一次bar改變的狀態
public class BaseActivity extends AppCompatActivity { private ImmersionBar mImmersionBar; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); mImmersionBar = ImmersionBar.with(this); mImmersionBar.init(); //全部子類都將繼承這些相同的屬性 } @Override protected void onDestroy() { super.onDestroy(); mImmersionBar.destroy(); //不調用該方法,若是界面bar發生改變,在不關閉app的狀況下,退出此界面再進入將記憶最後一次bar改變的狀態 } }
/** * 隱藏導航欄或狀態欄 * * @param barHide the bar hide * @return the immersion bar */ public ImmersionBar hideBar(BarHide barHide) { mBarParams.barHide = barHide; if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT || OSUtils.isEMUI3_1()) { if ((mBarParams.barHide == BarHide.FLAG_HIDE_NAVIGATION_BAR) || (mBarParams.barHide == BarHide.FLAG_HIDE_BAR)) { mBarParams.navigationBarColor = Color.TRANSPARENT; mBarParams.fullScreenTemp = true; } else { mBarParams.navigationBarColor = mBarParams.navigationBarColorTemp; mBarParams.fullScreenTemp = false; } } return this; }
第一種解決方案,監聽華爲虛擬按鈕,建議在baseActivity裏使用
@Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); immersionBar = ImmersionBar.with(this); immersionBar.init(); if (OSUtils.isEMUI3_1()) //解決華爲emui3.0與3.1手機手動隱藏底部導航欄時,導航欄背景色未被隱藏的問題 getContentResolver().registerContentObserver(Settings.System.getUriFor ("navigationbar_is_min"), true, mNavigationStatusObserver); } private ContentObserver mNavigationStatusObserver = new ContentObserver(new Handler()) { @Override public void onChange(boolean selfChange) { int navigationBarIsMin = Settings.System.getInt(getContentResolver(), "navigationbar_is_min", 0); if (navigationBarIsMin == 1) { //導航鍵隱藏了 immersionBar .transparentNavigationBar() .init(); } else { //導航鍵顯示了 immersionBar .navigationBarColor(android.R.color.black) .fullScreen(false) .init(); } } };
ImmersionBar.with(this) .navigationBarEnable(false) //禁止對導航欄相關設置 //或者 // .navigationBarWithKitkatEnable(false) //禁止對4.4設備導航欄相關設置 .init();
項目地址:
gyf-dev的博客地址:
博客地址:
http://blog.csdn.net/androidstarjack/article/details/76401105
若是你以爲此文對您有所幫助,歡迎關注 微信技術號:終端研發部