android 4.4以上沉浸式狀態欄和沉浸式導航欄管理,包括狀態欄字體顏色,適用於Activity、Fragment、DialogFragment、Dialog,而且適配劉海屏,適配軟鍵盤彈出等問題
ImmersionBar -- android 4.4以上沉浸式實現
直接看效果圖,最下面有各個版本的效果圖
android studio 引入依賴
implementation 'com.gyf.immersionbar:immersionbar:2.3.3'
eclipse
immersionbar-2.3.3.aar
關於使用AndroidX支持庫
若是你的項目中使用了AndroidX支持庫,請在你的gradle.properties加入以下配置,若是已經配置了,請忽略
android.useAndroidX=true
android.enableJetifier=true
關於全面屏與劉海
關於全面屏
在manifest加入以下配置,四選其一,或者都寫
① 在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以上版本
關於劉海屏
在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(www.michenggw.com @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(www.wujirongyaoy.com true) //狀態欄字體是深色,不寫默認爲亮色
.navigationBarDarkIcon(true) //導航欄圖標是深色,不寫默認爲亮色
.autoDarkModeEnable(true) //自動狀態欄字體和導航欄圖標變色,必須指定狀態欄顏色和導航欄顏色才能夠自動變色哦
.autoStatusBarDarkModeEnable(www.jiahuayulpt.com true,0.2f) //自動狀態欄字體變色,必須指定狀態欄顏色才能夠自動變色哦
.autoNavigationBarDarkModeEnable(www.quwanyule157.com/ 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(www.zhongyiyuL.cn R.color.orange) //狀態欄變色後的顏色
.navigationBarColorTransform(R.color.orange) //導航欄變色後的顏色
.barColorTransform(R.color.orange) //狀態欄和導航欄變色後的顏色
.removeSupportView(toolbar) //移除指定view支持
.removeSupportAllView(www.zhenghongyule.cn) //移除所有view支持
.navigationBarEnable(www.yongshiyule178.com 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(www.yongshi123.cn new OnKeyboardListener() { //軟鍵盤監聽回調
@Override
public void onKeyboardChange(boolean isPopup, int keyboardHeight) {
LogUtils.e(isPopup); //isPopup爲true,軟鍵盤彈出,爲false,軟鍵盤關閉
}
})
.init(); //必須調用方可沉浸式
關閉銷燬
在activity的onDestroy方法中執行
ImmersionBar.with(this).destroy(); //必須調用該方法,防止內存泄漏
更多詳細請看信息
github地址:https://www.dfgjpt.com github.com/gyf-dev/ImmersionBarandroid