ToolBar๋ฅผ ์ด์ฉํ์ฌ AppBar๋ฅผ ๊ตฌํํ๋ ์ค, ์๋๋ก์ด๋์์ ๊ธฐ๋ณธ์ ์ผ๋ก ์ ๊ณตํ๋ ToolBar ์์ค๋ฅผ ๋ฐ๊ฒฌํ๊ฒ ๋์๋ค.
๋จผ์ ์๋๋ก์ด๋ ์คํ๋์ค(Android Studio)๋ฅผ ์คํํ ๋ค ์ ํ๋ก์ ํธ๋ฅผ ์์ฑํด์ค๋ค.
๋ง์ฝ ํ๋ก์ ํธ๊ฐ ์ด๋ ค์๋ ์ํ๋ผ๋ฉด File → New → New Project๋ฅผ ํด๋ฆญํ๋ค.
๋ฏธ๋ฆฌ๋ณด๊ธฐ ์ฌ์ง์์ AppBar๊ฐ ์๋ Activity ์ค ์๋ฌด๊ฑฐ๋ ์ ํํ๋ฉด ๋์ง๋ง, ๋๋ ์ง๊ธ NavigationView, DrawerLayout ๋ฑ์ ๊ตฌํํ๊ณ ์๋ ์ํ์ด๊ธฐ ๋๋ฌธ์ Navigation Drawer Activity๋ฅผ ์ ํํ๋ค. Google Play๊ฐ ์ฐ์๋๋ ์๋๋ก์ด๋์ ๊ฐ์ฅ ๊ธฐ๋ณธ์ ์ธ ํํ์ธ ๊ฒ ๊ฐ๋ค.
์กํฐ๋นํฐ๋ฅผ ์ ํํ๊ณ Next → Finish ๋ฒํผ์ ์์ฐจ์ ์ผ๋ก ํด๋ฆญํ๋ฉด ์ฝ๊ฐ์ ๋น๋ ์๊ฐ์ ๊ฑฐ์ณ ์์ค ์ฝ๋๊ฐ ์ด๋ฆฌ๋๋ฐ, ํด๋ฐ๊ฐ ํฌํจ๋ xml(app_bar_main.xml)์ ์ด์ด๋ณด๋ฉด ๋ค์๊ณผ ๊ฐ๋ค.
app_bar_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/Theme.DrawerLayout.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/Theme.DrawerLayout.PopupOverlay" />
</com.google.android.material.appbar.AppBarLayout>
<include layout="@layout/content_main" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_marginEnd="@dimen/fab_margin"
android:layout_marginBottom="16dp"
app:srcCompat="@android:drawable/ic_dialog_email" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
XML์ ์ ์ฒด ๊ตฌ์กฐ๋ฅผ ๋ณด๋ฉด CoordinatorLayout์ด ๊ฐ์ฅ ๋ฐ๊นฅ์์ ๋ชจ๋ Layout์ ๊ฐ์ธ๊ณ ์๊ณ , ๊ทธ ์์ AppBarLayout, ๊ฐ์ฅ ์์ชฝ์ Toolbar๊ฐ ์์นํ๊ณ ์๋ค.
- Toolbar : ์ฑ๋ฐ๋ฅผ ๊ตฌ์ฑํ๋ ์ ํ๋ฆฌ์ผ์ด์ ์ ์ต์๋จ๋ฐ.
- include content_main : fragment๋ก ๊ตฌ์ฑ๋ ์ค์ ์ฝํ ์ธ ์์ญ.
- FloatingActionButton : ์ฐ์ธก ํ๋จ ์ก์ ๋ฒํผ. (ํ์ฌ ์ฝ๋์์๋ ๊ตฌํ๋์ง ์์)
content_main.xml
app_bar_main.xml์์ includeํ๋ ์ค์ ์ฝํ ์ธ ์์ญ์ ์ฝ๋์ด๋ค.
์ค์ ์ฝํ ์ธ ์์ญ์ fragment๋ก ๊ตฌํํ์ฌ ์ฌ๋ฌ ํ๋ฉด์ ๋ณด์ฌ์ค ์ ์๋๋ก ๊ตฌํ๋ ๋ชจ์ต์ด๋ค.
<?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:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/app_bar_main">
<fragment
android:id="@+id/nav_host_fragment_content_main"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/mobile_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
activity_main.xml
์์ฑํ ์ ๋ ์ฝ๋๋ฅผ ํ ๊ณณ์ผ๋ก ๋ชจ์์ค ๋ฉ์ธ XML์ด๋ค.
activity_main.xml → app_bar_main.xml → content_main.xml ์์๋ก includeํ๋ค.
<?xml version="1.0" encoding="utf-8"?>
<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_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />
</androidx.drawerlayout.widget.DrawerLayout>
์คํ ํ๋ฉด
์ ํ๋ฆฌ์ผ์ด์ ์๋จ์ ํด๋ฐ๊ฐ ์์ฑ๋ ๋ชจ์ต์ ํ์ธํ ์ ์๋ค.