1. 沉浸式實(shí)現(xiàn)原理就是使整個(gè)activity布局延伸到整個(gè)屏幕法挨,然后使?fàn)顟B(tài)欄變成透明色全闷,有些手機(jī)會(huì)有導(dǎo)航欄,同樣也可以把導(dǎo)航欄變成透明色赖瞒,這樣會(huì)使一些app更加美觀。
2.廢話不多說了蚤假,直接上代碼栏饮,步驟如下:
1、引入
1.1 github倉庫地址https://github.com/gyf-dev/ImmersionBar
1.2 gradle文件中加入
dependencies {compile'com.gyf.barlibrary:barlibrary:2.3.0'}
2 特性
1)基本介紹
基礎(chǔ)用法磷仰,建議在BaseActivity里調(diào)用
```
public class BaseActivity extends AppCompatActivity {
@Override
? protected void onCreate(@Nullable Bundle savedInstanceState) {
? ? ? super.onCreate(savedInstanceState);
? ? ? ? ImmersionBar.with(this).init(); //初始化袍嬉,默認(rèn)透明狀態(tài)欄和黑色導(dǎo)航欄
? ? }
? ? @Override
? ? protected void onDestroy() {
? ? ? ? super.onDestroy();
? ? ? ? ImmersionBar.with(this).destroy(); //不調(diào)用該方法,如果界面bar發(fā)生改變灶平,在不關(guān)閉app的情況下伺通,退出此界面再進(jìn)入將記憶最后一次bar改變的狀態(tài)
? ? }
}
```
到此已解決上圖所顯示的適配問題,就這么簡(jiǎn)單逢享,兩三行代碼搞定罐监。
3. 高級(jí)用法,如果基礎(chǔ)用法不能滿足你的需求瞒爬,可以試試這里的方法
```
ImmersionBar.with(this).transparentStatusBar()? //透明狀態(tài)欄弓柱,不寫默認(rèn)透明色
.transparentNavigationBar() //透明導(dǎo)航欄,不寫默認(rèn)黑色(設(shè)置此方法侧但,fullScreen()方法自動(dòng)為true)????
.transparentBar()? ? ?? //透明狀態(tài)欄和導(dǎo)航欄矢空,不寫默認(rèn)狀態(tài)欄為透明色,導(dǎo)航欄為黑色(設(shè)置此方法禀横,fullScreen()方法自動(dòng)為true)
.statusBarColor(R.color.colorPrimary)? ?? //狀態(tài)欄顏色屁药,不寫默認(rèn)透明色
.navigationBarColor(R.color.colorPrimary) //導(dǎo)航欄顏色,不寫默認(rèn)黑色
.barColor(R.color.colorPrimary) //同時(shí)自定義狀態(tài)欄和導(dǎo)航欄顏色柏锄,不寫默認(rèn)狀態(tài)欄為透明色者祖,導(dǎo)航欄為黑色
.statusBarAlpha(0.3f) //狀態(tài)欄透明度立莉,不寫默認(rèn)0.0f? ? ? ? ? ?
.navigationBarAlpha(0.4f) //導(dǎo)航欄透明度,不寫默認(rèn)0.0F? ? ? ? ? ? ? ?
.barAlpha(0.3f) //狀態(tài)欄和導(dǎo)航欄透明度七问,不寫默認(rèn)0.0f? ? ? ? ? ? ? ??
.statusBarDarkFont(true)? //狀態(tài)欄字體是深色蜓耻,不寫默認(rèn)為亮色? ? ? ?
.flymeOSStatusBarFontColor(R.color.btn3) //修改flyme OS狀態(tài)欄字體顏色? ? ? ? ? ? ? ??
.fullScreen(true)? ?? //有導(dǎo)航欄的情況下,activity全屏顯示械巡,也就是activity最下面被導(dǎo)航欄覆蓋刹淌,不寫默認(rèn)非全屏? ? ? ? ? ? ? ?
.hideBar(BarHide.FLAG_HIDE_BAR)? //隱藏狀態(tài)欄或?qū)Ш綑诨騼烧撸粚懩J(rèn)不隱藏? ? ? ? ? ? ? ?
.addViewSupportTransformColor(toolbar) //設(shè)置支持view變色讥耗,可以添加多個(gè)view有勾,不指定顏色,默認(rèn)和狀態(tài)欄同色古程,還有兩個(gè)重載方法
.titleBar(view)? ? //解決狀態(tài)欄和布局重疊問題蔼卡,任選其一? ? ? ? ?
.statusBarView(view)? //解決狀態(tài)欄和布局重疊問題,任選其一?
.fitsSystemWindows(true)?? //解決狀態(tài)欄和布局重疊問題挣磨,任選其一雇逞,默認(rèn)為false,當(dāng)為true時(shí)一定要指定statusBarColor()茁裙,不然狀態(tài)欄為透明色
.supportActionBar(true) //支持ActionBar使用? ? ? ? ? ?
.statusBarColorTransform(R.color.orange)? //狀態(tài)欄變色后的顏色? ? ? ? ? ? ? ??
.navigationBarColorTransform(R.color.orange) //導(dǎo)航欄變色后的顏色
.barColorTransform(R.color.orange)? //狀態(tài)欄和導(dǎo)航欄變色后的顏色? ? ? ? ? ? ? ??
.removeSupportView(toolbar)? //移除指定view支持? ? ? ? ? ? ? ??
.removeSupportAllView() //移除全部view支持
.addTag("tag")? //給以上設(shè)置的參數(shù)打標(biāo)記
.getTag("tag")? //根據(jù)tag獲得沉浸式參數(shù)? ? ? ? ? ? ? ? ? ? ? ?
.reset()? //重置所以沉浸式參數(shù)? ? ? ? ? ? ? ? ? ? ? ? ?
.keyboardEnable(true) //解決軟鍵盤與底部輸入框沖突問題塘砸,默認(rèn)為false? ? ? ? ? ? ? ? ? ? ? ? ? ?
.setOnKeyboardListener(new OnKeyboardListener() {? ? //軟鍵盤監(jiān)聽回調(diào)? ? ? ? ?
@Override
? ? public void onKeyboardChange(boolean isPopup, int keyboardHeight) {
? ? ? ? ? ? ? ? ? ? ?? LogUtils.e(isPopup);? //isPopup為true,軟鍵盤彈出晤锥,為false掉蔬,軟鍵盤關(guān)閉
? ? ? ? ? ? ? ? ?? }
? ? ? ? ? ? ? })
.keyboardMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE) //單獨(dú)指定軟鍵盤模式
.init();//必須調(diào)用方可沉浸式
```
補(bǔ)充:
4.4以下真正實(shí)現(xiàn)沉浸式可以使用統(tǒng)一的標(biāo)題欄布局效果圖和代碼類似如下:
include_title.xml:
、矾瘾、女轿、
?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
? ? android:id="@+id/title_bar"
? ? android:layout_width="match_parent"
? ? android:layout_height="64dp"
? ? android:background="?attr/colorPrimary"
? ? android:gravity="bottom"
? ? android:orientation="horizontal">
? ? ? ? android:id="@+id/ll_left"
? ? ? ? android:layout_width="0dp"
? ? ? ? android:layout_height="40dp"
? ? ? ? android:layout_weight="1.7"
? ? ? ? android:gravity="start|center_vertical"
? ? ? ? android:orientation="horizontal">
? ? ? ? ? ? android:id="@+id/tv_title_left"
? ? ? ? ? ? android:layout_width="match_parent"
? ? ? ? ? ? android:layout_height="match_parent"
? ? ? ? ? ? android:layout_marginLeft="4dp"
? ? ? ? ? ? android:gravity="center_vertical"
? ? ? ? ? ? android:maxLines="1"
? ? ? ? ? ? android:textColor="@color/white"
? ? ? ? ? ? android:textSize="14sp" />
? ? ? ? android:id="@+id/tv_title"
? ? ? ? android:layout_width="0dp"
? ? ? ? android:layout_height="40dp"
? ? ? ? android:layout_weight="3"
? ? ? ? android:ellipsize="end"
? ? ? ? android:gravity="center"
? ? ? ? android:maxLines="1"
? ? ? ? android:textColor="@color/white"
? ? ? ? android:textSize="18sp" />
? ? ? ? android:layout_width="0dp"
? ? ? ? android:layout_height="40dp"
? ? ? ? android:layout_weight="1.7"
? ? ? ? android:gravity="end|center_vertical">
? ? ? ? ? ? android:id="@+id/tv_title_right_copy"
? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="match_parent"
? ? ? ? ? ? android:text="復(fù)制手機(jī)號(hào)"
? ? ? ? ? ? android:drawablePadding="2dp"
? ? ? ? ? ? android:padding="5dp"
? ? ? ? ? ? android:gravity="center_vertical"
? ? ? ? ? ? android:textColor="@color/white"
? ? ? ? ? ? android:textSize="14sp"
? ? ? ? ? ? android:visibility="gone"/>
</LinearLayout>
、壕翩、蛉迹、
在宿主Acitivity中進(jìn)行引用
<include layout="@layout/include_title">
同時(shí)切記Activity中的布局中最外層不需要 添加android:fitsSystemWindows=”true”,不然不能實(shí)現(xiàn)沉浸式的效果戈泼。
加上android:fitsSystemWindows=”true”這句話之后布局文件就會(huì)自動(dòng)在狀態(tài)欄下開始布局,而不加這句并且在4.4以后加上
透明狀態(tài)欄之后你的activity的布局就會(huì)從整個(gè)手機(jī)屏幕頂端開始布局赏僧,而不是從狀態(tài)欄之后大猛。所以,加上透明狀態(tài)欄之后說明你的布局被上移了淀零,然后就
需要手動(dòng)來測(cè)量狀態(tài)欄的高度挽绩,并且給toolBar(我這里的布局,你的布局不確定哦)設(shè)置margin了驾中。
詳細(xì)介紹請(qǐng)參考博文:https://blog.csdn.net/ling9400/article/details/59478358
4唉堪、總結(jié)
? ? ? 至此模聋,ImmersionBar庫的基本用法與原理講完了。網(wǎng)上關(guān)于沉浸式的介紹鋪天蓋地唠亚,但是很少有人把它們封裝起來链方,當(dāng)開發(fā)者調(diào)用的時(shí)候還得自己去寫大量代碼,消耗大家時(shí)間灶搜。正好有時(shí)間親測(cè)管用祟蚀,也整理了一下方便呀以后的開發(fā),解決大家在沉浸式方面出現(xiàn)的問題割卖。如果還有不懂得地方可以去demo里看看前酿,或者直接底下留言!