본문 바로가기
Android

[Android/Kotlin] Navigation Drawer header 접근

by noddu 2022. 2. 27.
728x90
반응형

 

<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" />

 

NavigationView의 headerLayout인

 

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="@dimen/nav_header_height"
    android:background="@drawable/side_nav_bar"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:theme="@style/ThemeOverlay.AppCompat.Dark"
    android:orientation="vertical"
    android:gravity="bottom">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        app:srcCompat="@mipmap/ic_launcher_round"
        android:contentDescription="@string/nav_header_desc"
        android:id="@+id/imageView" />

    <TextView
        android:id="@+id/header_userUid"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        android:text="@string/nav_header_title"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

 

nav_header_main.xml의 TextView를 로그인 한 아이디값으로 set 해보겠다

 

 


 

 

val navView: NavigationView = binding.navView

먼저 Viewbinding으로 navView를 가져와 val navView로 선언하고

 

var header =  navView.getHeaderView(0)

navView.getHearderView를 하면 된다

 


var email = user?.email
var header_userUid : TextView = header.findViewById(R.id.header_userUid)
header_userEmail.text = email

나는 header에서 header_userUid의 TextView를 선언하고

setText해줬다

반응형