• 35648

    文章

  • 23

    评论

  • 20

    友链

  • 最近新加了很多技术文章,大家多来逛逛吧~~~~
  • 喜欢这个网站的朋友可以加一下QQ群,我们一起交流技术。

ImmersionBar 转

欢迎来到阿八个人博客网站。本 阿八个人博客 网站提供最新的站长新闻,各种互联网资讯。 喜欢本站的朋友可以收藏本站,或者加QQ:我们大家一起来交流技术! URL链接:https://www.abboke.com/jsh/2019/0703/4877.html

>>>

  • 项目地址: https://github.com/gyf-dev/ImmersionBar
  • 类别: 主题
  • 打分: ★★★★★
  • 更新: 2017-05-26 14:18
  • 大小: 144853 kb
  • 开发环境: Android Studio
  • 浏览: 8201 次
  • 下载: 499 次

gyf-dev / ImmersionBar

android 4.4以上沉浸式状态栏和沉浸式导航栏管理,适配横竖屏切换、刘海屏、软键盘弹出等问题,可以修改状态栏字体颜色和导航栏图标颜色,以及不可修改字体颜色手机的适配,适用于Activity、Fragment、DialogFragment、Dialog,PopupWindow,一句代码轻松实现,以及对bar的其他设置,详见README。简书请参考:http://www.jianshu.com/p/2a884e211a62

6,3611,054

介绍:

android 4.4以上沉浸式状态栏和沉浸式导航栏管理,一句代码轻松实现,以及对bar的其他设置

运行效果:

使用说明:

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

android studio

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

eclipse

barlibrary-2.1.9.jar

下载demo

下载

初始化

基础用法(已经可以满足日常沉浸式)

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

高级用法(每个参数的意义)

 
  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得高度

相关文章

暂住......别动,不想说点什么吗?
  • 全部评论(0
    还没有评论,快来抢沙发吧!