iT邦幫忙

2022 iThome 鐵人賽

DAY 10
0
Mobile Development

Android 開發 30天 初學之路筆記系列 第 10

Day10 - Android Navigation drawer 側拉導航欄

  • 分享至 

  • xImage
  •  

效果圖

步驟:

  1. 創建menu
  2. 創建navigation
  3. 依照menu創建對應的Fragment
  4. 創建DrawerLayout和NavigationView
  5. 創建Toolbar和fragment content
  6. 程式碼部分的設置

1. 創建menu

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:showIn="navigation_view">

    <group
        android:id="@+id/drawer_group1"
        android:checkableBehavior="single">
        <item
            android:id="@+id/nav_home"
            android:icon="@drawable/ic_menu_home"
            android:title="首頁"/>
        <item
            android:id="@+id/nav_favorite"
            android:icon="@drawable/ic_menu_favorite"
            android:title="我的收藏" />
        <item
            android:id="@+id/nav_person"
            android:icon="@drawable/ic_menu_person"
            android:title="個人檔案" />
        <item
            android:id="@+id/nav_question"
            android:icon="@drawable/ic_menu_question"
            android:title="客服中心" />
    </group>
    <group
        android:id="@+id/drawer_group2"
        android:checkableBehavior="single">
        <item
            android:id="@+id/nav_setting"
            android:title="設定" />
        <item
            android:id="@+id/nav_sign_out"
            android:title="登出" />
    </group>
</menu>
  • 使用Group區隔開item再分別添加id, 在畫面上會自動顯示分隔線。

2. 創建Navigation

在res>navigation創建main_navigation.xml

<?xml version="1.0" encoding="utf-8"?>
<navigation 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/main_navigation"
    app:startDestination="@id/nav_home">

    <fragment
        android:id="@+id/nav_home"
        android:name="com.example.itnavigationdrawerdemo2.HomeFragment"
        android:label="首頁"
        tools:layout="@layout/fragment_home" />
    <fragment
        android:id="@+id/nav_favorite"
        android:name="com.example.itnavigationdrawerdemo2.FavoriteFragment"
        android:label="我的收藏"
        tools:layout="@layout/fragment_favorite" />
    <fragment
        android:id="@+id/nav_person"
        android:name="com.example.itnavigationdrawerdemo2.PersonFragment"
        android:label="個人檔案"
        tools:layout="@layout/fragment_person" />
    <fragment
        android:id="@+id/nav_question"
        android:name="com.example.itnavigationdrawerdemo2.QuestionFragment"
        android:label="客服中心"
        tools:layout="@layout/fragment_question" />
</navigation>

4. 在主畫面創建DrawerLayout和NavigationView

activity_main.xml

<androidx.drawerlayout.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_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        android:id="@+id/app_bar_main"
        layout="@layout/app_bar_main"
        android:layout_height="match_parent"
        android:layout_width="match_parent" />

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        android:layout_gravity="start"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer"/>

</androidx.drawerlayout.widget.DrawerLayout>
  • DrawerLayout的tools:openDrawer和NavigationView的android:layout_gravity屬性是用來設置導航欄開啟的方向, 且它們的值必須相同才有效。

5. 創建Toolbar和fragment content

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
    </com.google.android.material.appbar.AppBarLayout>

    <fragment
        android:id="@+id/nav_host_fragment_content_main"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/appBarLayout"
        app:navGraph="@navigation/main_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>

6. 程式碼部分的設置

MainActivity.java

public class MainActivity extends AppCompatActivity {
    private ActivityMainBinding binding;
    private AppBarConfiguration mAppBarConfiguration;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        setSupportActionBar(binding.appBarMain.toolbar);

        DrawerLayout drawer = binding.drawerLayout;
        NavigationView navigationView = binding.navView;

        mAppBarConfiguration = new AppBarConfiguration.Builder(
                R.id.nav_home, R.id.nav_favorite, R.id.nav_person, R.id.nav_question)
                .setOpenableLayout(drawer)
                .build();

        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
        NavigationUI.setupWithNavController(navigationView, navController);
    }

    @Override
    public boolean onSupportNavigateUp() {
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
        return NavigationUI.navigateUp(navController, mAppBarConfiguration) || super.onSupportNavigateUp();
    }
}

上一篇
Day09 - 新版 Logcat v2 | Android Studio Dolphin
下一篇
Day11 - Android Navigation component
系列文
Android 開發 30天 初學之路筆記30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言