博客原文:http://www.jcodecraeer.com/a/opensource/2017/0526/7976.html
github地址:https://github.com/gyf-dev/ImmersionBar
介紹:
運行效果:

使用說明:
相關文章:http://www.jianshu.com/p/2a884e211a62
android studio
- compile 'com.gyf.barlibrary:barlibrary:2.1.9'
eclipse
下載demo
初始化
基礎用法(已經可以滿足日常沉浸式)
- ImmersionBar.with(this).init();
高級用法(每個參數的意義)
- 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) //狀態欄字體是深色,不寫默認為亮色
- .fullScreen(true) //有導航欄的情況下,activity全屏顯示,也就是activity最下面被導航欄覆蓋,不寫默認非全屏
- .hideBar(BarHide.FLAG_HIDE_BAR) //隱藏狀態欄或導航欄或兩者,不寫默認不隱藏
- .setViewSupportTransformColor(toolbar) //設置支持view變色,支持一個view,不指定顏色,默認和狀態欄同色,還有兩個重載方法
- .addViewSupportTransformColor(toolbar) //設置支持view變色,可以添加多個view,不指定顏色,默認和狀態欄同色,還有兩個重載方法
- .statusBarView(view) //解決狀態欄和布局重疊問題
- .fitsSystemWindows(false) //解決狀態欄和布局重疊問題,默認為false,當為true時一定要指定statusBarColor(),不然狀態欄為透明色
- .statusBarColorTransform(R.color.orange) //狀態欄變色后的顏色
- .navigationBarColorTransform(R.color.orange) //導航欄變色后的顏色
- .barColorTransform(R.color.orange) //狀態欄和導航欄變色后的顏色
- .removeSupportView() //移除通過setViewSupportTransformColor()方法指定的view
- .removeSupportView(toolbar) //移除指定view支持
- .removeSupportAllView() //移除全部view支持
- .init(); //必須調用方可沉浸式
關閉銷毀
在activity的onDestroy方法中執行
- ImmersionBar.with(this).destroy(); //不調用該方法,如果界面bar發生改變,在不關閉app的情況下,退出此界面再進入將記憶最后一次bar改變的狀態
建議
建議在BaseActivity中初始化和銷毀
- public class BaseActivity extends AppCompatActivity {
- @Override
- protected void onCreate(@Nullable Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- ImmersionBar.with(this).init();
- }
- @Override
- protected void onDestroy() {
- super.onDestroy();
- ImmersionBar.with(this).destroy(); //不調用該方法,如果界面bar發生改變,在不關閉app的情況下,退出此界面再進入將記憶最后一次bar改變的狀態
- }
- }
在Fragment中的用法(fragment+viewpager)
為了使每個fragment都可以設置不同的沉浸式樣式,這里給出兩種解決方式,這兩種實現效果都一樣的
①使用viewpager的addOnPageChangeListener方法,代碼如下
- viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
- @Override
- public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
- }
- @Override
- public void onPageSelected(int position) {
- ImmersionBar immersionBar = ImmersionBar.with(FragmentActivity.this);
- switch (position) {
- case 0:
- immersionBar.statusBarDarkFont(false)
- .navigationBarColor(R.color.btn4)
- .init();
- break;
- case 1:
- immersionBar.statusBarDarkFont(true)
- .navigationBarColor(R.color.btn3)
- .init();
- break;
- case 2:
- immersionBar.statusBarDarkFont(false)
- .navigationBarColor(R.color.btn13)
- .init();
- break;
- case 3:
- immersionBar.statusBarDarkFont(true)
- .navigationBarColor(R.color.btn1)
- .init();
- break;
- }
- }
- @Override
- public void onPageScrollStateChanged(int state) {
- }
- });
②繼承ImmersionFragment類,在immersionInit中初始化沉浸式,代碼如下:
- public class OneFragment extends ImmersionFragment {
- @Nullable
- @Override
- public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
- return inflater.inflate(R.layout.fragment_one, container, false);
- }
- @Override
- protected void immersionInit() {
- ImmersionBar.with(getActivity())
- .statusBarDarkFont(false)
- .navigationBarColor(R.color.btn4)
- .init();
- }
- }
狀態欄與布局頂部重疊解決方案,四種方案任選其一
① 使用dimen自定義狀態欄高度
在values-v19/dimens.xml文件下
- <dimen name="status_bar_height">25dp</dimen>
在values/dimens.xml文件下
- <dimen name="status_bar_height">0dp</dimen>
然后在布局界面添加view標簽,高度指定為status_bar_height
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="@color/darker_gray"
- android:orientation="vertical">
- <View
- android:layout_width="match_parent"
- android:layout_height="@dimen/status_bar_height"
- android:background="@color/colorPrimary" />
- <android.support.v7.widget.Toolbar
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:background="@color/colorPrimary"
- app:title="方法一"
- app:titleTextColor="@android:color/white" />
- </LinearLayout>
② 使用系統的fitsSystemWindows屬性
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- android:fitsSystemWindows="true">
- </LinearLayout>
然后使用ImmersionBar時候必須指定狀態欄顏色
③ 使用ImmersionBar的fitsSystemWindows(boolean fits)方法
- ImmersionBar.with(this)
- .statusBarColor(R.color.colorPrimary)
- .fitsSystemWindows(true) //使用該屬性必須指定狀態欄的顏色,不然狀態欄透明,很難看
- .init();
④ 使用ImmersionBar的statusBarView(View view)方法
在標題欄的上方增加View標簽,高度指定為0dp
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="@color/darker_gray"
- android:orientation="vertical">
- <View
- android:layout_width="match_parent"
- android:layout_height="0dp"
- android:background="@color/colorPrimary" />
- <android.support.v7.widget.Toolbar
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:background="@color/colorPrimary"
- app:title="方法四"
- app:titleTextColor="@android:color/white" />
- </LinearLayout>
然后使用ImmersionBar的statusBarView方法,指定view就可以啦
- ImmersionBar.with(this)
- .statusBarView(view)
- .init();
解決EditText和軟鍵盤的問題
- KeyboardPatch.patch(this, linearLayout).enable(); //解決底部EditText和軟鍵盤的問題,linearLayout指的是當前布局的根節點
當白色背景狀態欄遇到不能改變狀態欄字體為深色的設備時,解決方案
- if(ImmersionBar.isSupportStatusBarDarkFont()){ //判斷當前設備支不支持狀態欄字體變色
- //處理狀態欄字體為黑色
- }else {
- //處理狀態欄有透明度
- }
狀態欄和導航欄其它方法
-
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得高度