android 4.4以上沉浸式状态栏和沉浸式导航栏管理,包括状态栏字体颜色,适用于Activity、Fragment、DialogFragment、Dialog,而且适配刘海屏,适配软键盘弹出等问题html
android studio 引入依赖java
implementation 'com.gyf.immersionbar:immersionbar:2.3.3'
eclipseandroid
android.useAndroidX=true android.enableJetifier=true
在manifest加入以下配置,四选其一,或者都写github
① 在manifest的Application节点下加入app
<meta-data android:name="android.max_aspect" android:value="2.4" />
② 在manifest的Application节点中加入eclipse
android:resizeableActivity="true"
③ 在manifest的Application节点中加入ide
android:maxAspectRatio="2.4"
④ 升级targetSdkVersion为25以上版本布局
在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" />
基础用法,建议在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(); } @Override protected void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); // 若是你的app能够横竖屏切换,而且适配4.4或者emui3手机请务必在onConfigurationChanged方法里添加这句话 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(); //必须调用该方法,防止内存泄漏
更多详细请看信息