๐Ÿ’ป Language/Android : ์•ˆ๋“œ๋กœ์ด๋“œ

[Android] CoordinatorLayout์„ ์ด์šฉํ•œ ToolBar ๋งŒ๋“ค๊ธฐ

mxnxeonx 2022. 4. 5. 17:11
728x90
728x90

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>

 

 

์‹คํ–‰ ํ™”๋ฉด

์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ƒ๋‹จ์— ํˆด๋ฐ”๊ฐ€ ์ƒ์„ฑ๋œ ๋ชจ์Šต์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

728x90
320x100