在Android 原生樣式應用中,有一個特別漂亮的效果,在使用抽屜佈局的時候,展開或關閉抽屜時,Toolbar的 navigation drawer(upButton) 有一個動畫,由三個橫線旋轉成箭頭,大概如下:
靜態圖片展示不出來動畫效果,請自行補腦!
這個效果其實是由 Toolbar+DrawerLayout 實現的。
可以通過以下幾步實現:
- 在佈局中加入DrawerLayout
- 在代碼中找到DrawerLayout,將DrawerLayout 與 Toolbar關聯
- 通過樣式修改 navigation drawer 的顏色
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:id="@+id/toolbar"
android:background="#339999" />
<android.support.v4.widget.DrawerLayout
android:layout_below="@+id/toolbar"
android:id="@+id/drawerlayou"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--Main Content-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#123456">
</LinearLayout>
<!--DrawerLayout Content-->
<LinearLayout
android:layout_width="300dp"
android:layout_height="match_parent"
android:background="#345678"
android:layout_gravity="start" />
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
final DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawerlayou);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.app_name, R.string.app_name);
toggle.syncState();
drawerLayout.setDrawerListener(toggle);
//通過 NavigationDrawer 打開關閉 抽屜
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "HOME", Toast.LENGTH_SHORT).show();
if (drawerLayout.isDrawerOpen(Gravity.START))
drawerLayout.closeDrawer(Gravity.START);
else
drawerLayout.openDrawer(Gravity.START);
}
});
<resources>
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
<item name="colorPrimary">#66cc99</item>
<item name="colorPrimaryDark">#66cc99</item>
<item name="android:windowBackground">@color/back</item>
<!-- 使用自定義樣式-->
<item name="drawerArrowStyle">@style/AppTheme.MyDrawerArrowStyle</item>
</style>
<style name="AppTheme" parent="AppTheme.Base" />
<!--自定義 navigation drarwer 的樣式-->
<style name="AppTheme.MyDrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
<item name="spinBars">false</item>
<item name="color">#FFFFFF</item>
</style>
</resources>
如果想要抽屜遮住 Toolbar,只需要改下佈局,將Toolbar放入DrawerLayout的主界面即可:
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawerlayou"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--Main Content-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#123456">
<!-- android:elevation="5dp" 加上陰影-->
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:id="@+id/toolbar"
android:elevation="5dp"
android:background="#339999" />
</LinearLayout>
<!--DrawerLayout Content-->
<LinearLayout
android:layout_width="300dp"
android:layout_height="match_parent"
android:background="#345678"
android:layout_gravity="start" />
</android.support.v4.widget.DrawerLayout>