試試這個: 第1步: 我們需要添加依賴的設計支持庫。添加以下依賴項。
compile 'com.android.support:design:23.2.0'
第2步: 創建菜單文件夾內的文件menu_navigation.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="@+id/home"
android:title="Home"
android:icon="@drawable/ic_home"/>
<item
android:id="@+id/settings"
android:title="Settings"
android:icon="@drawable/ic_setting"/>
<item
android:id="@+id/trash"
android:title="Trash"
android:icon="@drawable/ic_trash"/>
<item
android:id="@+id/logout"
android:title="Logout"
android:icon="@drawable/ic_exit"/>
</group>
</menu>
第3步: 佈局文件夾中創建nav_header.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:paddingTop="20dp"
android:paddingBottom="20dp"
android:background="@color/colorPrimaryDark"
android:layout_width="match_parent"
android:layout_height="190dp">
<ImageView
android:src="@drawable/ic_person"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:layout_height="0dp" />
<TextView
android:id="@+id/tv_email"
android:textColor="@color/White"
android:textSize="18sp"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
第4步: 裏面你activity_main.xml粘貼這個
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.learn2crack.myapplication.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/Call Your Layout" />
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_gravity="start"
app:headerLayout="@layout/nav_header"
app:menu="@menu/menu_navigation"/>
</android.support.v4.widget.DrawerLayout>
第5步:
<string name="drawer_open">Open</string>
<string name="drawer_close">Close</string>
步驟6:
裏面你MainActivity類別
粘貼並調用它裏面的onCreate。
public void initNavigationDrawer() {
NavigationView navigationView = (NavigationView)findViewById(R.id.navigation_view);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
int id = menuItem.getItemId();
switch (id){
case R.id.home:
Toast.makeText(getApplicationContext(),"Home",Toast.LENGTH_SHORT).show();
drawerLayout.closeDrawers();
break;
case R.id.settings:
Toast.makeText(getApplicationContext(),"Settings",Toast.LENGTH_SHORT).show();
break;
case R.id.trash:
Toast.makeText(getApplicationContext(),"Trash",Toast.LENGTH_SHORT).show();
drawerLayout.closeDrawers();
break;
case R.id.logout:
finish();
}
return true;
}
});
View header = navigationView.getHeaderView(0);
TextView tv_email = (TextView)header.findViewById(R.id.tv_email);
tv_email.setText("Any String");
drawerLayout = (DrawerLayout)findViewById(R.id.drawer);
ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.drawer_open,R.string.drawer_close){
@Override
public void onDrawerClosed(View v){
super.onDrawerClosed(v);
}
@Override
public void onDrawerOpened(View v) {
super.onDrawerOpened(v);
}
};
drawerLayout.addDrawerListener(actionBarDrawerToggle);
actionBarDrawerToggle.syncState();
}
如需詳細資料請參閱本網站 https://www.learn2crack.com/2016/03/android-material-design-sliding-navigation-drawer.html
你可以分享你的代碼或logcat的? –
如何不使用模板,只是學習如何將DrawerLayout + NavigationView集成到代碼中?如果您有錯誤,請顯示您的代碼並詢問有關錯誤。換句話說,如果應用程序崩潰,是的,有錯誤 –
你好,我已經添加了我的主要活動的代碼,我想整合抽屜在該特定的活動,或所有其他人,我必須手動實現它? – Lalati