Переместите относительный макет (дизайн заголовка) в другой файл макета: header.xml
<ImageView
android:id="@+id/profile_image"
android:visibility="invisible"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:contentDescription="@string/nav_header_desc"
app:srcCompat="@mipmap/ic_launcher_round" />
<TextView
android:id="@+id/user_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/profile_image"
android:layout_toLeftOf="@id/profile_image"
android:gravity="center"
android:text="@string/nav_header_subtitle" />
<TextView
android:id="@+id/user_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@id/user_email"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@id/profile_image"
android:fontFamily="@font/uifont"
android:gravity="center"
android:paddingTop="@dimen/nav_header_vertical_spacing"
android:text="@string/nav_header_title"
android:textAppearance="@style/TextAppearance.AppCompat.Body1" />
</RelativeLayout>
И на экране ящика:
<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
layout="@layout/app_bar_home"
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/header"
app:menu="@menu/activity_home_drawer" >
</com.google.android.material.navigation.NavigationView>
</androidx.drawerlayout.widget.DrawerLayout>
ВВаш Java-файл, чтобы получить доступ к представлениям заголовка, который находится внутри макета заголовка:
val headerView: View = nav_view.getHeaderView(0)
headerView.user_name.text = "Some Value"