更改 NavigationView 的标题标题

Moh*_*uag 4 android android-layout navigation-drawer

我有一个NavigationView嵌套在DrawerLayout.

在此处输入图片说明

活动_main.xml

<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
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="match_parent"
android:fitsSystemWindows="true">

.......

<android.support.design.widget.NavigationView
    android:id="@+id/navigation_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:headerLayout="@layout/drawer_header"
    app:menu="@menu/drawer"/>
</android.support.v4.widget.DrawerLayout>
Run Code Online (Sandbox Code Playgroud)

drawer_header.xml

<?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="150dp"
              android:background="?attr/colorPrimaryDark"
              android:gravity="bottom"
              android:orientation="vertical"
              android:padding="16dp"
              android:theme="@style/ThemeOverlay.AppCompat.Dark">

    <TextView
        android:id="@+id/drawerHeaderTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/drawer_header_text"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1"/>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

如何在 Java 代码中访问抽屉标题的标题?我试过了:

  1. 直接使用它的iddrawerHeaderTitle)访问它,但我得到了NullPointerException

  2. 通过访问它NavigationView.findViewById:相同的错误

我怎样才能克服这个问题?

Moh*_*ami 5

使用这种方法

 mNavigationView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
            @Override
            public void onLayoutChange(View view, int i, int i1, int i2, int i3, int i4, int i5, int i6, int i7) {

                mNavigationView.removeOnLayoutChangeListener( this );

                TextView textView = (TextView) mNavigationView.findViewById(R.id.drawerHeaderTitle);

                textView.setText("title");
            }
        });
Run Code Online (Sandbox Code Playgroud)