ImmersionBar

gyf-dev / ImmersionBar

android 4.4以上沉浸式狀態欄和沉浸式導航欄管理,適配橫豎屏切換、劉海屏、軟鍵盤彈出等問題,能夠修改狀態欄字體顏色和導航欄圖標顏色,以及不可修改字體顏色手機的適配,適用於Activity、Fragment、DialogFragment、Dialog,PopupWindow,一句代碼輕鬆實現,以及對bar的其餘設置,詳見README。簡書請參考:http://www.jianshu.com/p/2a884e211a62php

6,3611,054android

介紹:

android 4.4以上沉浸式狀態欄和沉浸式導航欄管理,一句代碼輕鬆實現,以及對bar的其餘設置git

運行效果:

使用說明:

相關文章:http://www.jianshu.com/p/2a884e211a62 github

android studioapp

 
  1. compile 'com.gyf.barlibrary:barlibrary:2.1.9'

eclipseeclipse

barlibrary-2.1.9.jaride

下載demo

下載佈局

初始化

基礎用法(已經能夠知足平常沉浸式)字體

 
  1. ImmersionBar.with(this).init();

高級用法(每一個參數的意義)this

 
  1.  ImmersionBar.with(this)
  2.              .transparentStatusBar()  //透明狀態欄,不寫默認透明色
  3.              .transparentNavigationBar()  //透明導航欄,不寫默認黑色(設置此方法,fullScreen()方法自動爲true)
  4.              .transparentBar()             //透明狀態欄和導航欄,不寫默認狀態欄爲透明色,導航欄爲黑色(設置此方法,fullScreen()方法自動爲true)
  5.              .statusBarColor(R.color.colorPrimary)     //狀態欄顏色,不寫默認透明色
  6.              .navigationBarColor(R.color.colorPrimary) //導航欄顏色,不寫默認黑色
  7.              .barColor(R.color.colorPrimary)  //同時自定義狀態欄和導航欄顏色,不寫默認狀態欄爲透明色,導航欄爲黑色
  8.              .statusBarAlpha(0.3f)  //狀態欄透明度,不寫默認0.0f
  9.              .navigationBarAlpha(0.4f)  //導航欄透明度,不寫默認0.0F
  10.              .barAlpha(0.3f)  //狀態欄和導航欄透明度,不寫默認0.0f
  11.              .statusBarDarkFont(true)   //狀態欄字體是深色,不寫默認爲亮色
  12.              .fullScreen(true)      //有導航欄的狀況下,activity全屏顯示,也就是activity最下面被導航欄覆蓋,不寫默認非全屏
  13.              .hideBar(BarHide.FLAG_HIDE_BAR)  //隱藏狀態欄或導航欄或二者,不寫默認不隱藏
  14.              .setViewSupportTransformColor(toolbar) //設置支持view變色,支持一個view,不指定顏色,默認和狀態欄同色,還有兩個重載方法
  15.              .addViewSupportTransformColor(toolbar)  //設置支持view變色,能夠添加多個view,不指定顏色,默認和狀態欄同色,還有兩個重載方法
  16.              .statusBarView(view)  //解決狀態欄和佈局重疊問題
  17.              .fitsSystemWindows(false)    //解決狀態欄和佈局重疊問題,默認爲false,當爲true時必定要指定statusBarColor(),否則狀態欄爲透明色
  18.              .statusBarColorTransform(R.color.orange)  //狀態欄變色後的顏色
  19.              .navigationBarColorTransform(R.color.orange) //導航欄變色後的顏色
  20.              .barColorTransform(R.color.orange)  //狀態欄和導航欄變色後的顏色
  21.              .removeSupportView()  //移除經過setViewSupportTransformColor()方法指定的view
  22.              .removeSupportView(toolbar)  //移除指定view支持
  23.              .removeSupportAllView() //移除所有view支持
  24.              .init();  //必須調用方可沉浸式

關閉銷燬

在activity的onDestroy方法中執行

 
  1. ImmersionBar.with(this).destroy(); //不調用該方法,若是界面bar發生改變,在不關閉app的狀況下,退出此界面再進入將記憶最後一次bar改變的狀態

建議

建議在BaseActivity中初始化和銷燬

 
  1. public class BaseActivity extends AppCompatActivity {
  2.      @Override
  3.      protected void onCreate(@Nullable Bundle savedInstanceState) {
  4.          super.onCreate(savedInstanceState);
  5.          ImmersionBar.with(this).init();
  6.      }
  7.  
  8.      @Override
  9.      protected void onDestroy() {
  10.          super.onDestroy();
  11.          ImmersionBar.with(this).destroy();  //不調用該方法,若是界面bar發生改變,在不關閉app的狀況下,退出此界面再進入將記憶最後一次bar改變的狀態
  12.      }
  13.  }

在Fragment中的用法(fragment+viewpager)

爲了使每一個fragment均可以設置不一樣的沉浸式樣式,這裏給出兩種解決方式,這兩種實現效果都同樣的

①使用viewpager的addOnPageChangeListener方法,代碼以下

 
  1.  viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
  2.          @Override
  3.          public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
  4.  
  5.          }
  6.  
  7.          @Override
  8.          public void onPageSelected(int position) {
  9.              ImmersionBar immersionBar = ImmersionBar.with(FragmentActivity.this);
  10.              switch (position) {
  11.                  case 0:
  12.                      immersionBar.statusBarDarkFont(false)
  13.                              .navigationBarColor(R.color.btn4)
  14.                              .init();
  15.                      break;
  16.                  case 1:
  17.                      immersionBar.statusBarDarkFont(true)
  18.                              .navigationBarColor(R.color.btn3)
  19.                              .init();
  20.                      break;
  21.                  case 2:
  22.                      immersionBar.statusBarDarkFont(false)
  23.                              .navigationBarColor(R.color.btn13)
  24.                              .init();
  25.                      break;
  26.                  case 3:
  27.                      immersionBar.statusBarDarkFont(true)
  28.                              .navigationBarColor(R.color.btn1)
  29.                              .init();
  30.                      break;
  31.              }
  32.          }
  33.  
  34.          @Override
  35.          public void onPageScrollStateChanged(int state) {
  36.  
  37.          }
  38.      });

②繼承ImmersionFragment類,在immersionInit中初始化沉浸式,代碼以下:

 
  1. public class OneFragment extends ImmersionFragment {
  2.      @Nullable
  3.      @Override
  4.         public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  5.             return inflater.inflate(R.layout.fragment_one, container, false);
  6.         }
  7.         @Override
  8.         protected void immersionInit() {
  9.             ImmersionBar.with(getActivity())
  10.                     .statusBarDarkFont(false)
  11.                     .navigationBarColor(R.color.btn4)
  12.                     .init();
  13.         }
  14.     }

狀態欄與佈局頂部重疊解決方案,四種方案任選其一

① 使用dimen自定義狀態欄高度

在values-v19/dimens.xml文件下

 
  1.     <dimen name="status_bar_height">25dp</dimen>

在values/dimens.xml文件下

 
  1.  <dimen name="status_bar_height">0dp</dimen>

而後在佈局界面添加view標籤,高度指定爲status_bar_height

 
  1.    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.        xmlns:app="http://schemas.android.com/apk/res-auto"
  3.        android:layout_width="match_parent"
  4.        android:layout_height="match_parent"
  5.        android:background="@color/darker_gray"
  6.        android:orientation="vertical">
  7.    
  8.        <View
  9.            android:layout_width="match_parent"
  10.            android:layout_height="@dimen/status_bar_height"
  11.            android:background="@color/colorPrimary" />
  12.    
  13.        <android.support.v7.widget.Toolbar
  14.            android:layout_width="match_parent"
  15.            android:layout_height="wrap_content"
  16.            android:background="@color/colorPrimary"
  17.            app:title="方法一"
  18.            app:titleTextColor="@android:color/white" />
  19.    </LinearLayout>

② 使用系統的fitsSystemWindows屬性

 
  1.     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.         android:layout_width="match_parent"
  3.         android:layout_height="match_parent"
  4.         android:orientation="vertical"
  5.         android:fitsSystemWindows="true">
  6.     </LinearLayout>

而後使用ImmersionBar時候必須指定狀態欄顏色

③ 使用ImmersionBar的fitsSystemWindows(boolean fits)方法

 
  1.     ImmersionBar.with(this)
  2.         .statusBarColor(R.color.colorPrimary)
  3.         .fitsSystemWindows(true)  //使用該屬性必須指定狀態欄的顏色,否則狀態欄透明,很難看
  4.         .init();

④ 使用ImmersionBar的statusBarView(View view)方法

在標題欄的上方增長View標籤,高度指定爲0dp

 
  1.     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.            xmlns:app="http://schemas.android.com/apk/res-auto"
  3.            android:layout_width="match_parent"
  4.            android:layout_height="match_parent"
  5.            android:background="@color/darker_gray"
  6.            android:orientation="vertical">
  7.        
  8.            <View
  9.                android:layout_width="match_parent"
  10.                android:layout_height="0dp"
  11.                android:background="@color/colorPrimary" />
  12.        
  13.            <android.support.v7.widget.Toolbar
  14.                android:layout_width="match_parent"
  15.                android:layout_height="wrap_content"
  16.                android:background="@color/colorPrimary"
  17.                app:title="方法四"
  18.                app:titleTextColor="@android:color/white" />
  19.     </LinearLayout>

而後使用ImmersionBar的statusBarView方法,指定view就能夠啦

 
  1.      ImmersionBar.with(this)
  2.            .statusBarView(view)
  3.            .init();

解決EditText和軟鍵盤的問題

 
  1.       KeyboardPatch.patch(this, linearLayout).enable(); //解決底部EditText和軟鍵盤的問題,linearLayout指的是當前佈局的根節點

當白色背景狀態欄遇到不能改變狀態欄字體爲深色的設備時,解決方案

 
  1.       if(ImmersionBar.isSupportStatusBarDarkFont()){ //判斷當前設備支不支持狀態欄字體變色
  2.           //處理狀態欄字體爲黑色
  3.       }else {
  4.           //處理狀態欄有透明度
  5.       }

狀態欄和導航欄其它方法

  • public static boolean hasNavigationBar(Activity activity)

    判斷是否存在導航欄

  • public static int getNavigationBarHeight(Activity activity)

    得到導航欄的高度

  • public static int getNavigationBarWidth(Activity activity)

    得到導航欄的寬度

  • public static boolean isNavigationAtBottom(Activity activity)

    判斷導航欄是否在底部

  • public static int getStatusBarHeight(Activity activity)

    或得狀態欄的高度

  • public static int getActionBarHeight(Activity activity)

    或得ActionBar得高度

相關文章
相關標籤/搜索