Android:无法在 NavigationView 标签内获取 TextView

标签 android findviewbyid navigationview

我的 NavigationView 标签内有一个 TextView。但是我无法通过 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

最佳答案

我想做的是在我的抽屉导航中添加一个页脚以显示应用程序版本。由于应用程序版本是动态数据,因此在导航 View 下方添加静态 TextView 并用外部布局包装它们也行不通。解决问题的正确方法如下链接所述:

NavigationView with Foother

主要思想是在 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>

关于Android:无法在 NavigationView 标签内获取 TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35363648/

相关文章:

android - 导航组件 : IllegalStateException Fragment not associated with a fragment manager

java - findViewById 用于在包含的合并根元素内查看

java - findViewById 从 Activity 返回 null

Android以编程方式查找ViewByID

android - 如何使用 Expandable 关闭 NavigationView 中的组菜单

ios - SwiftUI 导航链接后退按钮在预览中有效,但在模拟器或设备中无效

android - 我不明白 MVVM 上的 "instance has not been initialized"

android - 在 onCreate 中使用 PersistableBundle 时,它​​显示一个空白页面

android - 图像 Intent 和后按

android - 将 addtextchangedlistener 用于 NavigationView Edittext 时出错