项目地址: https://github.com/gyf-dev/ImmersionBar

gyf-dev / ImmersionBar

android 4.4以上沉浸式状态栏和沉浸式导航栏管理,包括状态栏字体颜色,一句代码轻松实现,以及对bar的其他设置,详见README,http://www.jianshu.com/p/2a884e211a62

3,105522

介绍:

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. @Override
  8. protected void onDestroy() {
  9. super.onDestroy();
  10. ImmersionBar.with(this).destroy();  //不调用该方法,如果界面bar发生改变,在不关闭app的情况下,退出此界面再进入将记忆最后一次bar改变的状态
  11. }
  12. }

在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. @Override
  6. public void onPageSelected(int position) {
  7. ImmersionBar immersionBar = ImmersionBar.with(FragmentActivity.this);
  8. switch (position) {
  9. case 0:
  10. immersionBar.statusBarDarkFont(false)
  11. .navigationBarColor(R.color.btn4)
  12. .init();
  13. break;
  14. case 1:
  15. immersionBar.statusBarDarkFont(true)
  16. .navigationBarColor(R.color.btn3)
  17. .init();
  18. break;
  19. case 2:
  20. immersionBar.statusBarDarkFont(false)
  21. .navigationBarColor(R.color.btn13)
  22. .init();
  23. break;
  24. case 3:
  25. immersionBar.statusBarDarkFont(true)
  26. .navigationBarColor(R.color.btn1)
  27. .init();
  28. break;
  29. }
  30. }
  31. @Override
  32. public void onPageScrollStateChanged(int state) {
  33. }
  34. });

②继承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. <View
  8. android:layout_width="match_parent"
  9. android:layout_height="@dimen/status_bar_height"
  10. android:background="@color/colorPrimary" />
  11. <android.support.v7.widget.Toolbar
  12. android:layout_width="match_parent"
  13. android:layout_height="wrap_content"
  14. android:background="@color/colorPrimary"
  15. app:title="方法一"
  16. app:titleTextColor="@android:color/white" />
  17. </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. <View
  8. android:layout_width="match_parent"
  9. android:layout_height="0dp"
  10. android:background="@color/colorPrimary" />
  11. <android.support.v7.widget.Toolbar
  12. android:layout_width="match_parent"
  13. android:layout_height="wrap_content"
  14. android:background="@color/colorPrimary"
  15. app:title="方法四"
  16. app:titleTextColor="@android:color/white" />
  17. </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得高

ImmersionBar相关推荐

  1. Immersionbar学习笔记

    一.什么是Immersionbar? ​ Immersionbar是一个第三方的类库,用于 android 4.4以上沉浸式实现. ​ 什么是沉浸式?前一张图是正常未使用沉浸式的,后一张图使用沉浸式后 ...

  2. ImmersionBar(状态栏和导航栏)

    用法 1.依赖: implementation 'com.gyf.immersionbar:immersionbar:2.3.3' 2.初始化:

  3. 沉浸式状态栏ImmersionBar

    引入 ``implementation 'com.gyf.immersionbar:immersionbar:3.0.0' //导航栏 mImmersionBar = ImmersionBar.wit ...

  4. ImmersionBar篇Android实现全屏配置 自动横屏

    ImmersionBar篇Android实现全屏配置 自动横屏 项目下的gradle配置添加 // 基础依赖包,必须要依赖 implementation 'com.gyf.immersionbar:i ...

  5. Android专题-常用第三方框架

    Android专题-常用第三方框架 HTTP网络请求 带*号的是个人推荐比较好用的 HTTP网络请求 okhttp * :https://github.com/square/okhttp retrof ...

  6. android button 添加事件_2019最新Android常用开源库总结

    前言 收集了一些比较常见的开源库,特此记录(已收录350+).另外,本文将持续更新,大家有关于Android 优秀的开源库,也可以在下面留言. 一 .基本控件 TextView HTextView 一 ...

  7. android studio 显示view树_Android 沉浸式解析和轮子使用

    文 | 黑羽 on 移动电商 前言 我们先一起来回顾一下实现沉浸式状态栏的一般套路.在 Android 上,关于对 StatusBar(状态栏)的操作,一直都在不断改善,并且表现越来越好,在 Andr ...

  8. android封装网络请求界面,轻松搞定 android MVP 架构、okHttp 网络模块封装 的 项目...

    CommonMvp commonMvp 能做什么? 1.mvp 实现 model view presenter 业务和界面解耦 2.整合 网络 请求 3.简化网络调用流程 4.整合状态栏和标题栏 实现 ...

  9. 固定导航栏android,Android 状态栏和导航栏的真终极解决方案

    去年我写过一篇文章,透明状态栏和导航栏的终极解决方案,并在 Github 上开源了代码,https://github.com/Zackratos/UltimateBar,其实在那之后,我一直对这个项目 ...

最新文章

  1. mysql导入sql脚本出现there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE
  2. Matlab入门笔记
  3. Javascript 绑定事件和 this理解
  4. 公司内部 action调用持久层规范
  5. linux curl 命令 http请求、下载文件、ftp上传下载
  6. springboot中的mybatis是如果使用pagehelper的
  7. XVII Open Cup named after E.V. Pankratiev. GP of Tatarstan
  8. Git 在推送(Push)信息的时候提示git did not exit cleanly (exit code 1)的解决办法
  9. 【详谈 Delta Lake 】系列技术专题 之 特性(Features)
  10. 【基于注解方式】Spring整合Kafka
  11. DevExpress v18.2版本亮点——Office File API 篇
  12. formdata 嵌套_解决form嵌套
  13. 符合c语言语法规定的是,若变量已正确定义并赋值,符合C语言语法的表达式是 A。a=a+7; B.a=7+b+c,a++...
  14. 万字长文带你快速了解并上手Testcontainers
  15. JAVA中dot的用法_doT学习(一)之语法
  16. 用axure做产品需求文档
  17. 5G前传从无源到半有源平滑演进解决方案
  18. 华为matex搭载鸿蒙系统,华为 MateX 推迟至 9 月上市,或预装鸿蒙系统
  19. Android手机界面组成
  20. 地形系统shader学习

热门文章

  1. 2021年美容师(初级)考试总结及美容师(初级)考试试题
  2. 06_02_任务三:Spring JDBCTemplate 声明式事务
  3. kaldi群北京线下交流会(2017年4月9日)
  4. 555定时器的基本原理和应用案例
  5. 【应用SLAM技术建立二维栅格化地图】
  6. 当过服务员、快递员,现在年薪30W,历尽山河叛逆少年终会成长
  7. linux下怎么监控网络 io swap,监控io性能,free命令,ps命令,查看网络状态,linux下抓包(示例代码)...
  8. 分享一个找找外文书籍和资源(源代码)的网站
  9. STM32F4通过U盘升级程序
  10. 解决哈希(hash)冲突的方法