注意:以下效果實(shí)現(xiàn)方法只在Android Lollipop下完成页眯,
并假設(shè)你使用Theme.AppCompat.NoActionBar税稼,
或者是Theme.AppCompat.Light.NoActionBar主題
Material Design有個(gè)很酷炫的效果讥蟆,就是DrawerLayout
隱藏的那一部分在拉開的時(shí)候,本應(yīng)該被ActionBar
和status bar擋住的那部分爵赵,現(xiàn)在可以露出來了扛或。
這種效果已有人給出效果的實(shí)現(xiàn)方法了,但是對于很多人來說還是不知道它是如何實(shí)現(xiàn)的瓢颅,所以這個(gè)這里先講講原理:
在新的框架和支持包下恩尾,我們可以實(shí)現(xiàn)以下功能:首先使用Toolbar
來代替ActionBar
,這樣我們就能夠把ActionBar嵌入到我們的View體系中挽懦,然后我們"禁用"系統(tǒng)的status bar特笋,由DrawerLayout
來處理status bar,最后抽屜部分往上移巾兆,或者裁剪掉status bar那一部分猎物。
控制status bar
在你的values-v21里面添加新的主題,并設(shè)置一下屬性:
values-v21/themes.xml
<style name="AppTheme">
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">@android:color/transparent</item>
</style>
這里解釋一下:
windowDrawsSystemBarBackgrounds
角塑,將它設(shè)置為true蔫磨,系統(tǒng)將在你的window里面繪制status bar,默認(rèn)為TRUE圃伶,之所以要寫出來是因?yàn)槟愕膖heme有可能是繼承過來的堤如,確保為true。(在這里小插曲一下窒朋,因調(diào)試時(shí)搀罢,總以為注釋了這段代碼就以為是false,程序員思維害苦了我侥猩。另外從命名來看榔至,Android把它稱為system bar,可能是為了與能被我們處理的status bar區(qū)分開而做的改變欺劳。)
statusBarColor
設(shè)置為透明是因?yàn)槲覀儾辉傩枰到y(tǒng)的status bar唧取,因?yàn)槲覀儫o法控制它的位置,后面我們將交由DrawerLayout
來處理划提。
使用DrawerLayout
首先枫弟,你的布局文件應(yīng)該是和這個(gè)類似的:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/my_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<!-- Your normal content view -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- We use a Toolbar so that our drawer can be displayed
in front of the action bar -->
<android.support.v7.widget.Toolbar
android:id="@+id/my_awesome_toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary" />
<!-- The rest of your content view -->
</LinearLayout>
<!-- The navigation drawer -->
<ScrimInsetsFrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="304dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="@android:color/white"
android:elevation="10dp"
android:fitsSystemWindows="true"
app:insetForeground="#4000">
<!-- Your drawer content -->
</ScrimInsetsFrameLayout>
</android.support.v4.widget.DrawerLayout>
在這里布局里面我們用到了一個(gè)的開源類ScrimInsetsFrameLayout,它的主要作用就是利用fitsSystemWindows
的回調(diào)方法fitSystemWindows(Rect insets)
來獲取status bar的大小鹏往,然后調(diào)整畫布以達(dá)到去掉status bar的效果淡诗,所以我們需要在ScrimInsetsFrameLayout
下設(shè)置fitsSystemWindows
為true。當(dāng)然你也可以不使用這個(gè)類,而改用layout_marginTop
屬性來達(dá)到效果韩容。
insetForeground
這個(gè)屬性是ScrimInsetsFrameLayout
自帶的绪爸,表示插入?yún)^(qū)域的前景色,我們設(shè)置為帶透明的黑色#4000宙攻。別忘了使用這個(gè)屬性需要添加如下代碼到attrs.xml里:
values/attrs.xml
<declare-styleable name="ScrimInsetsView">
<attr name="insetForeground" format="reference|color" />
</declare-styleable>
自此奠货,我們已經(jīng)實(shí)現(xiàn)了將DrawerLayout
抽屜的那一部分顯示在Toolbar
和system bar(為了和下面的status bar區(qū)分,我們稱為system bar)之間了座掘,可是system bar的顏色被我們設(shè)置了透明递惋,所以我們接下來要改變status bar的顏色。
改變status bar的顏色
你可能已經(jīng)注意到剛才的布局里面DrawerLayout
的fitsSystemWindows
屬性設(shè)置了為true溢陪,這是因?yàn)槲覀円诖a里面使用了DrawerLayout
設(shè)置status bar顏色的方法:
// 在這里我們獲取了主題暗色萍虽,并設(shè)置了status bar的顏色
TypedValue typedValue = new TypedValue();
getTheme().resolveAttribute(R.attr.colorPrimaryDark, typedValue, true);
int color = typedValue.data;
// 注意setStatusBarBackgroundColor方法需要你將fitsSystemWindows設(shè)置為true才會生效
DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.my_drawer_layout);
drawerLayout.setStatusBarBackgroundColor(color);
有關(guān)獲取attr屬性值的方法,可以看我另外一篇文章形真,點(diǎn)這里杉编。
使用Toolbar來代替ActionBar
在代碼里面這樣設(shè)置:
Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
setSupportActionBar(toolbar);
最后
趕緊看看你出來的效果是不是和上面的一樣?為了實(shí)現(xiàn)這一點(diǎn)效果并理解它咆霜,真要認(rèn)真琢磨一下邓馒,最后感謝你的閱讀到這里。