學(xué)習(xí)使用Material Design控件(二)使用DrawerLayout實現(xiàn)側(cè)滑菜單欄效果
本文介紹如何使用DrawerLayout和NavigationView實現(xiàn)側(cè)滑菜單欄的效果。
效果如下:

Layout布局
<android.support.v4.widget.DrawerLayout xmlns:android=“http://schemas.android.com/apk/res/android” xmlns:app=“http://schemas.android.com/apk/res-auto” android:id=“@+id/drawer_layout” android:layout_width=“match_parent” android:layout_height=“match_parent” android:fitsSystemWindows=“true”> <android.support.design.widget.NavigationView android:id=“@+id/navigation_view” android:layout_width=“wrap_content” android:layout_height=“match_parent” android:layout_gravity=“start” app:headerLayout=“@layout/navigation_header” app:menu=“@menu/drawer” /> </android.support.v4.widget.DrawerLayout>
NavigationView需要設(shè)置app:headerLayout 和 app:menu,headerLayout對應(yīng)菜單的上面部分,一般用來顯示用戶信息,menu則對應(yīng)實際的菜單項文件。
headerLayout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="192dp" android:background="?attr/colorPrimaryDark" android:gravity="center" android:orientation="vertical" android:padding="16dp" android:theme="@style/ThemeOverlay.AppCompat.Dark"> <de.hdodenhof.circleimageview.CircleImageView android:id="@+id/profile_image" android:layout_width="72dp" android:layout_height="72dp" android:layout_marginTop="20dp" android:src="@mipmap/profile" app:border_color="@color/primary_light" app:border_width="2dp" /> <TextView android:layout_marginTop="10dp" android:textSize="18sp" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="APP開發(fā)者" android:gravity="center" android:textAppearance="@style/TextAppearance.AppCompat.Body1" /> </LinearLayout>
menu
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity"> <group android:checkableBehavior="single"> <item android:id="@+id/navigation_item_example" android:icon="@drawable/ic_favorite" android:title="@string/navigation_example" /> <item android:id="@+id/navigation_item_blog" android:icon="@drawable/ic_favorite" android:title="@string/navigation_my_blog" /> <item android:id="@+id/navigation_item_about" android:icon="@drawable/ic_favorite" android:title="@string/navigation_about" /> </group> </menu>
代碼實現(xiàn)
ActionBarDrawerToggle可以配合Toolbar,實現(xiàn)Toolbar上菜單按鈕開關(guān)效果。
//設(shè)置Toolbar
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
//設(shè)置DrawerLayout
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar,
R.string.drawer_open, R.string.drawer_close);
mDrawerToggle.syncState();
mDrawerLayout.setDrawerListener(mDrawerToggle);
//設(shè)置NavigationView點擊事件
mNavigationView = (NavigationView) findViewById(R.id.navigation_view);
setupDrawerContent(mNavigationView);
//設(shè)置NavigationView點擊事件
private void setupDrawerContent(NavigationView navigationView) {
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.navigation_item_example:
switchToExample();
break;
case R.id.navigation_item_blog:
switchToBlog();
break;
case R.id.navigation_item_about:
switchToAbout();
break;
}
menuItem.setChecked(true);
mDrawerLayout.closeDrawers();
return true;
}
});
}
項目源碼已發(fā)布到Github,以后慢慢加入其他控件的使用。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android 使用FragmentTabHost實現(xiàn)底部菜單功能
這篇文章主要介紹了Android 使用FragmentTabHost實現(xiàn)底部菜單功能,詳細給大家介紹了FragmentTabHost配合Fragment的使用方法,需要的朋友可以參考下2017-12-12
Android手勢密碼view學(xué)習(xí)筆記(一)
這篇文章主要為大家詳細介紹了Android手勢密碼view的學(xué)習(xí)筆記,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-03-03
Android使用BottomNavigationBar實現(xiàn)導(dǎo)航欄功能
這篇文章主要介紹了Android使用BottomNavigationBar實現(xiàn)導(dǎo)航欄功能,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2018-08-08
Android開發(fā)之利用Activity實現(xiàn)Dialog對話框
這篇文章主要給大家介紹了Android開發(fā)之如何利用Activity實現(xiàn)Dialog對話框效果,文中給出了詳細的示例代碼,相信對大家的理解及學(xué)習(xí)具有一定的參考借鑒價值,有需要的朋友們下面來一起看看吧。2016-12-12
詳解Android 在 ViewPager 中使用 Fragment 的懶加載
本篇文章主要介紹了Android 在 ViewPager 中使用 Fragment 的懶加載,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06
Android 中FloatingActionButton(懸浮按鈕)實例詳解
這篇文章主要介紹了Android 中FloatingActionButton(懸浮按鈕)實例詳解的相關(guān)資料,需要的朋友可以參考下2017-05-05

