MainActivity.java
public class MainActivity extends AppCompatActivity {
Toolbar mToolbar;
DrawerLayout mDrawerLayout;
NavigationView mNavigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.navigation_drawer);
mToolbar = findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
mDrawerLayout = findViewById(R.id.drawer_layout);
mNavigationView = findViewById(R.id.navigation_view);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar, R.string.open_drawer, R.string.close_drawer);
mDrawerLayout.setDrawerListener(toggle);
toggle.syncState();
}
}
的 activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:id="@+id/toolbar"
android:background="?attr/colorPrimary"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
</RelativeLayout>
navigation_drawer.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include layout="@layout/activity_main"/>
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/navigation_view"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/navigation_header"
app:menu="@menu/navigation_menu">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
navigation_header.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="180dp"
android:background="@drawable/header"
android:padding="16dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark"
android:gravity="bottom">
<android.support.v7.widget.AppCompatImageView
android:layout_width="56dp"
android:layout_height="56dp"
android:src="@drawable/goku_image"
android:scaleType="centerCrop"
android:contentDescription="@string/profile_image"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Goku"
android:paddingTop="8dp"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="gokusaiyangod@cc.com"
android:paddingTop="4dp"/>
</LinearLayout>
navigation_menu.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/inbox_id"
android:title="@string/inbox"
android:icon="@drawable/inbox"/>
<item
android:id="@+id/starred_id"
android:title="@string/starred"
android:icon="@drawable/starred"/>
<item
android:id="@+id/sent_id"
android:title="@string/sent_mail"
android:icon="@drawable/sent"/>
<item
android:id="@+id/drafts_id"
android:title="@string/drafts"
android:icon="@drawable/drafts"/>
</group>
<item android:title="@string/more_labels">
<menu>
<item
android:id="@+id/allMail_id"
android:title="@string/all_mail"
android:icon="@drawable/allmail"/>
<item
android:id="@+id/trash_id"
android:title="@string/trash"
android:icon="@drawable/trash"/>
<item
android:id="@+id/spam_id"
android:title="@string/spam"
android:icon="@drawable/spam"/>
</menu>
</item>
</menu>
当我向navigation_drawer提供navigation_menu和navigation_header时,它不能在物理设备上工作,但如果没有提供这两个,它可以正常工作。并且正如标题所说,它与#34;菜单&#34;和#34;标题&#34;当我在模拟器上运行时,它提供给navigation_drawer,但它不能在物理设备上运行。