一、概述
为了满足所有继承BaseActivity的页面都能简单的实现是否全屏显示。
二、实现
ImmersionBar官方简介:
android 4.4以上沉浸式状态栏和沉浸式导航栏管理,适配横竖屏切换、刘海屏、软键盘弹出等问题,可以修改状态栏字体颜色和导航栏图标颜色,以及不可修改字体颜色手机的适配,适用于Activity、Fragment、DialogFragment、Dialog,PopupWindow,一句代码轻松实现。
导入依赖:
dependencies {
implementation 'com.geyifeng.immersionbar:immersionbar:3.2.2'
}
官方给的指导用法:
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.x手机导航栏颜色,默认为true
.navigationBarWithEMUI3Enable(true) //是否可以修改emui3.x手机导航栏颜色,默认为true
.keyboardEnable(true) //解决软键盘与底部输入框冲突问题,默认为false,还有一个重载方法,可以指定软键盘mode
.keyboardMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE) //单独指定软键盘模式
.setOnKeyboardListener(new OnKeyboardListener() { //软键盘监听回调,keyboardEnable为true才会回调此方法
@Override
public void onKeyboardChange(boolean isPopup, int keyboardHeight) {
LogUtils.e(isPopup); //isPopup为true,软键盘弹出,为false,软键盘关闭
}
})
.setOnNavigationBarListener(onNavigationBarListener) //导航栏显示隐藏监听,目前只支持华为和小米手机
.setOnBarListener(OnBarListener) //第一次调用和横竖屏切换都会触发,可以用来做刘海屏遮挡布局控件的问题
.addTag("tag") //给以上设置的参数打标记
.getTag("tag") //根据tag获得沉浸式参数
.reset() //重置所以沉浸式参数
.init(); //必须调用方可应用以上所配置的参数
啰嗦的插句关于用法,在你想要实现沉浸式的界面代码文件中:
protected ImmersionBar mImmersionBar;
mImmersionBar = ImmersionBar.with(this);
//根据自己的需要添加上需要的方法如
mImmersionBar.transparentStatusBar();
//最后通过.init()方法载入
mImmersionBar.init();
当然,我使用这个轮子只是为了给我的BaseActivity类加上一个通用可以简单控制是否是全屏来显示页面,但是在这样的基础类中使用ImmersionBar.init方法后会导致一件事,就是原本的StateBar的高度塌缩了,像是这样:
像是之前我一直的处理方式都是在最外部的layout中加上个id,通过id来找到该布局设置个LayoutParams或者是直接通过设置PaddingTop一个任务栏的高度来解决的,当然各位可以选择自己所方便用的方法来处理,但是这次是想解决每次都要加上一个没有别的用处的id来更简便的处理这个问题,经过一个下午的寻找,终于想到。
if (heightCompensation()){
ViewGroup rootView = findViewById(android.R.id.content);
rootView.setPadding(0,ImmersionBar.getStatusBarHeight(this),0,0);
}
嘿嘿嘿,直接调用每个继承这个BaseActivity的根布局在最顶部设置一个Padding值,外部通过重写heightCompensation这个方法的返回值来确定要不要实现高度补偿。也算是不繁琐的解决这个问题。下面放出完整代码块:
public abstract class BaseActivity<K extends ViewDataBinding> extends AppCompatActivity {
protected K mBinding;
protected ImmersionBar mImmersionBar;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mBinding = DataBindingUtil.setContentView(this, getLayoutId());
if (initStatusBar()){
mImmersionBar = ImmersionBar.with(this).statusBarDarkFont(setStatusBarDarkFont(),1.0f).statusBarColor(getBarColor());
// 是否为沉浸式状态栏
if (fullScreen()){
mImmersionBar.transparentStatusBar();
}
mImmersionBar.init();
// 高度补偿机制
if (heightCompensation()){
ViewGroup rootView = findViewById(android.R.id.content);
rootView.setPadding(0,ImmersionBar.getStatusBarHeight(this),0,0);
}
}
init();
}
/**
* 在此销毁部分方法以防内存泄露
*/
@Override
protected void onDestroy() {
super.onDestroy();
}
protected abstract int getLayoutId();
protected void init() {
}
/**
*是否为沉浸式
*/
protected boolean fullScreen(){
return false;
}
/**
* 是否初始化状态栏
*/
protected boolean initStatusBar(){
return true;
}
/**
* 状态栏是否为黑色字体,默认为ture
*
* @return 是否让状态栏取用黑色字体
*/
protected boolean setStatusBarDarkFont() {
return true;
}
protected int getBarColor() {
return R.color.common_titleBar_color;
}
/**
* 高度补偿,作为使用immersionBar的一个高度补偿机制,这样就不需要在top的view设置一个pram或者是一个padding值
* @return
*/
protected boolean heightCompensation(){
return true;
}
}
public class MainActivity extends BaseActivity<ActivityMainBinding> {
@Override
protected int getLayoutId() {
return R.layout.activity_main;
}
@Override
protected boolean heightCompensation() {
return true;
}
@Override
protected boolean fullScreen() {
return true;
}
}
最后的实现效果: