先載入dependency
dependencies {implementation 'com.google.android.material:material:1.0.0'}
設計activity_main.xml最外框是DrawerLayer,包覆著LinearLayout。LinearLayout 為 一 Action Bar + Fragment 要切換的區域
<androidx.drawerlayout.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">
<!-- This LinearLayout represents the contents of the screen -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"><!-- The ActionBar displayed at the top -->
<include
layout="@layout/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" /><!-- The main content view where fragments are loaded -->
<FrameLayout
android:id="@+id/flContent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout><!-- The navigation drawer that comes from the left -->
<!-- Note that `android:layout_gravity` needs to be set to 'start' -->
<com.google.android.material.navigation.NavigationView
android:id="@+id/nvView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@android:color/white"
app:headerLayout="@layout/nav_header"
app:menu="@menu/drawer_view" />
</androidx.drawerlayout.widget.DrawerLayout
透過app:headerLayout="@layout/nav_header"
來加入header圖像的部分在runtime 時期將會inflat為
NavigationView navigationView = (NavigationView) findViewById(R.id.nvView);
View headerLayout = navigationView.inflateHeaderView(R.layout.nav_header);ImageView ivHeaderPhoto = headerLayout.findViewById(R.id.imageView);
app:menu="@menu/drawer_view" />
來加入選單內容
設計ToolBar
<?xml version="1.0" encoding="utf-8"?><androidx.appcompat.widget.Toolbarxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"android:id="@+id/toolbar"android:layout_height="wrap_content"android:layout_width="match_parent"android:fitsSystemWindows="true"android:minHeight="?attr/actionBarSize"app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"android:background="?attr/colorPrimaryDark"></androidx.appcompat.widget.Toolbar>
在styles.xml 中調整ActionBar 為<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
如果忘了調整styles.xml , 會引發 error
java.lang.RuntimeException: Unable to start activity ComponentInfo{tw.com.ian.mydrawer/tw.com.ian.mydrawer.MainActivity}: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
設計header
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="192dp"android:background="?attr/colorPrimaryDark"android:padding="16dp"android:theme="@style/ThemeOverlay.AppCompat.Dark"android:orientation="vertical"android:gravity="bottom"><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/running"/><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="史艷文"android:textColor="@android:color/white"android:textAppearance="@style/TextAppearance.AppCompat.Body1"/></LinearLayout>
設計menu
<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android"><group android:checkableBehavior="single"><itemandroid:id="@+id/medical_fragment"android:icon="@drawable/ic_one"android:title="醫學院" /><itemandroid:id="@+id/chat_fragment"android:icon="@drawable/ic_two"android:title="聊天室" /><itemandroid:id="@+id/game_fragment"android:icon="@drawable/ic_three"android:title="遊戲室" /><item android:title="其他"><menu><group android:checkableBehavior="single"><itemandroid:title="設定" /><itemandroid:title="登入" /></group></menu></item></group></menu>
到此,layout 設計完成。接下來撰寫activity的內容
第4行的toolbar 就是之前設計的toolbar
10~16行綁定DrawerLayout 與Toolbar
19–58 行設定menu 被觸發時要執行的動作
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// This will display an Up icon (<-), we will replace it with hamburger later
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// Find our drawer view
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.drawer_open, R.string.drawer_close);
toggle.setDrawerIndicatorEnabled(true);
toggle.syncState();
// Tie DrawerLayout events to the ActionBarToggle
drawer.addDrawerListener(toggle);
navigationView = (NavigationView) findViewById(R.id.nvView);
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
Fragment fragment = null;
Class fragmentClass;
switch(menuItem.getItemId()) {
case R.id.medical_fragment:
fragmentClass = MedicalFragment.class;
break;
case R.id.chat_fragment:
fragmentClass = ChatFragment.class;
break;
case R.id.game_fragment:
fragmentClass = GameFragment.class;
break;
default:
fragmentClass = MedicalFragment.class;
}
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
// Insert the fragment by replacing any existing fragment
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();
// Highlight the selected item has been done by NavigationView
menuItem.setChecked(true);
// Set action bar title
setTitle(menuItem.getTitle());
// Close the navigation drawer
drawer.closeDrawers();
return true;
}
});
}