Android刘海屏适配 ----- ImmersionBar

android 4.4以上沉浸式状态栏和沉浸式导航栏管理,适配横竖屏切换、刘海屏、软键盘弹出等问题,可以修改状态栏字体颜色和导航栏图标颜色,以及不可修改字体颜色手机的适配,适用于Activity、Fragment、DialogFragment、Dialog,PopupWindow;

  1. 引入

    // 基础依赖包,必须要依赖 implementation 'com.gyf.immersionbar:immersionbar:3.0.0' // fragment快速实现(可选) implementation 'com.gyf.immersionbar:immersionbar-components:3.0.0' // kotlin扩展(可选) implementation 'com.gyf.immersionbar:immersionbar-ktx:3.0.0
  2. 关于全面屏与刘海

    ① 在manifest的Application节点下加入

     <meta-data 
         android:name="android.max_aspect"
         android:value="2.4" />

    ② 在manifest的Application节点中加入

    android:resizeableActivity="true"

    ③ 在manifest的Application节点中加入

     android:maxAspectRatio="2.4"

    ④ 升级targetSdkVersion为25以上版本

  3. 关于刘海屏

    在manifest的Application节点下加入,vivo和oppo没有找到相关配置信息

       <!--适配华为(huawei)刘海屏-->
       <meta-data 
         android:name="android.notch_support" 
         android:value="true"/>
       <!--适配小米(xiaomi)刘海屏-->
       <meta-data
         android:name="notch.config"
         android:value="portrait|landscape" />

    用法

    初始化

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

      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)   //状态栏字体是深色,不写默认为亮色
                   .navigationBarDarkIcon(true) //导航栏图标是深色,不写默认为亮色
                   .autoDarkModeEnable(true) //自动状态栏字体和导航栏图标变色,必须指定状态栏颜色和导航栏颜色才可以自动变色哦
                   .autoStatusBarDarkModeEnable(true,0.2f) //自动状态栏字体变色,必须指定状态栏颜色才可以自动变色哦
                   .autoNavigationBarDarkModeEnable(true,0.2f) //自动导航栏图标变色,必须指定导航栏颜色才可以自动变色哦
                   .flymeOSStatusBarFontColor(R.color.btn3)  //修改flyme OS状态栏字体颜色
                   .fullScreen(true)      //有导航栏的情况下,activity全屏显示,也就是activity最下面被导航栏覆盖,不写默认非全屏
                   .hideBar(BarHide.FLAG_HIDE_BAR)  //隐藏状态栏或导航栏或两者,不写默认不隐藏
                   .addViewSupportTransformColor(toolbar)  //设置支持view变色,可以添加多个view,不指定颜色,默认和状态栏同色,还有两个重载方法
                   .titleBar(view)    //解决状态栏和布局重叠问题,任选其一
                   .titleBarMarginTop(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
                   .keyboardMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)  //单独指定软键盘模式
                   .setOnKeyboardListener(new OnKeyboardListener() {    //软键盘监听回调
                         @Override
                         public void onKeyboardChange(boolean isPopup, int keyboardHeight) {
                             LogUtils.e(isPopup);  //isPopup为true,软键盘弹出,为false,软键盘关闭
                         }
                    })
                   .init();  //必须调用方可沉浸式

      关闭销毁

      • 在activity的onDestroy方法中执行

        ImmersionBar.with(this).destroy(); //必须调用该方法,防止内存泄漏

      建议

      • 建议在BaseActivity中初始化和销毁

        public class BaseActivity extends AppCompatActivity {
        
             @Override
             protected void onCreate(@Nullable Bundle savedInstanceState) {
                 super.onCreate(savedInstanceState);
                 // 所有子类都将继承这些相同的属性,请在设置界面之后设置
                 ImmersionBar.with(this).init();  
             }
           
             @Override
             protected void onResume() {
                 super.onResume();
                 // 非必加
                 // 如果你的app可以横竖屏切换,适配了华为emui3系列系统手机,并且navigationBarWithEMUI3Enable为true,
                 // 请在onResume方法里添加这句代码(同时满足这三个条件才需要加上代码哦:1、横竖屏可以切换;2、华为emui3系列系统手机;3、navigationBarWithEMUI3Enable为true)
                 // 否则请忽略
                 if (OSUtils.isEMUI3_x()) {
                     ImmersionBar.with(this).init();   
                 }   
             }
         
             @Override
             protected void onDestroy() {
                 super.onDestroy();
                 // 必须调用该方法,防止内存泄漏
                 ImmersionBar.with(this).destroy();  
             }
           
            @Override
             protected void onConfigurationChanged(Configuration newConfig) {
                 super.onConfigurationChanged(newConfig);
                 // 非必加
                 // 如果你的app可以横竖屏切换,适配了4.4或者华为emui3.1系统手机,并且navigationBarWithKitkatEnable为true,
                 // 请务必在onConfigurationChanged方法里添加如下代码(同时满足这三个条件才需要加上代码哦:1、横竖屏可以切换;2、android4.4或者华为emui3.1系统手机;3、navigationBarWithKitkatEnable为true)
                 // 否则请忽略
                 ImmersionBar.with(this).init();   
             }
         }

        Demo下载地址:https://github.com/gyf-dev/ImmersionBar

        By:杨

         

猜你喜欢

转载自www.cnblogs.com/widgetbox/p/11959436.html