Android: невозможно получить TextView внутри тега NavigationView

У меня есть TextView внутри тега NavigationView. Но я не могу получить это методом findViewById:

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:itemIconTint="@color/drawer_item"
    app:itemTextColor="@color/drawer_item"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/activity_main_drawer">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/version_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="20dp"
            android:layout_marginLeft="20dp"
            android:layout_marginStart="20dp" />

    </RelativeLayout>

</android.support.design.widget.NavigationView>

Я пробовал следующие способы, но всегда получаю null.

findViewById(R.id.version_name); //null
navigationView.findViewById(R.id.version_name); //null
navigationView.getRootView.findViewById(R.id.version_name); //null

person Olcay Ertaş    schedule 12.02.2016    source источник
comment
stackoverflow.com/questions/33161345/   -  person Gabriele Mariotti    schedule 12.02.2016
comment
Возможный дубликат stackoverflow .com/questions/33199764/   -  person Jay Rathod RJ    schedule 12.02.2016


Ответы (2)


у меня такая же проблема..

View header = LayoutInflater.from(this).inflate(R.layout.nav_header_main, null);
navigationView.addHeaderView(header);
TextView text = (TextView) header.findViewById(R.id.textView);
texto.setText("Your text");
person Raja Jawahar    schedule 12.02.2016

Что я пытался сделать, так это добавить нижний колонтитул в свой ящик навигации, чтобы показать версию приложения. Поскольку версия приложения представляет собой динамические данные, добавление статического текстового представления в представление навигации ниже и обертывание их внешним макетом также не может работать. Правильный подход к решению проблемы описан ниже по ссылке:

NavigationView с нижним колонтитулом

Основная идея заключается в том, чтобы поместить два объекта NavigationView в NavigationView: один для обычных элементов навигации, а второй — для элементов нижнего колонтитула.

<android.support.v4.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/layout_dashboard"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">

    <!-- Activity content goes here -->

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_drawer_container"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start">

        <android.support.design.widget.NavigationView
            android:id="@+id/navigation_drawer"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="top"
            app:menu="@menu/menu_navigation_drawer" />

        <android.support.design.widget.NavigationView
            android:id="@+id/navigation_drawer_bottom"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            app:menu="@menu/menu_navigation_drawer_bottom" />

    </android.support.design.widget.NavigationView>

</android.support.v4.widget.DrawerLayout>
person Olcay Ertaş    schedule 08.10.2016