상세 컨텐츠

본문 제목

[안드로이드] DrawerLayout Navigation Header 추가하기

안드로이드

by aries574 2021. 1. 20. 10:11

본문



이번 시간에는 이전에 DrawerLayout Navigation에 Header를 넣어보겠습니다.

이전에 했던 소스 그대로 이어서 하겠습니다.


1. header 파일 만들기(header_layout.xml)

파일명은 header_layout로 했습니다.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="192dp"
android:orientation="vertical"
android:background="?attr/colorPrimaryDark"
android:gravity="bottom"
android:padding="16dp"
>

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="My header title"
android:gravity="center"
android:textColor="@android:color/white"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"/>

</LinearLayout>


2. 메인화면 적용(activity_main.xml)

app:headerLayout 에 방금 전에 만든 header_layout파일을 적용했습니다.

<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="@+id/drawer_layout"
android:fitsSystemWindows="true"
>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<include layout="@layout/toolbar_layout"/>
</LinearLayout>

<com.google.android.material.navigation.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:id="@+id/navigationView"
android:layout_gravity="left"
app:menu="@menu/drawer_menu"
app:headerLayout="@layout/header_layout"
/>

</androidx.drawerlayout.widget.DrawerLayout>


3. 실행화면



반응형

관련글 더보기

댓글 영역