2016-02-12 1 views
0

J'ai un TextView dans mon tag NavigationView. Mais je ne peux pas l'obtenir avec findViewById méthode:Android: impossible d'obtenir TextView à l'intérieur de la balise NavigationView

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

J'ai essayé suivant façons, mais j'obtenir toujours null.

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

http://stackoverflow.com/questions/33161345/android-support-v23-1-0-update-breaks-navigationview-get-find-header/33163288#33163288 –

+0

double possible de http://stackoverflow.com/questions/33199764/android-api-23-change-navigation-view-headerlayout-textview –

Répondre

0

Ce que je cherchais à faire est d'ajouter un foother à mon tiroir de navigation pour afficher la version de l'application. Étant donné que la version de l'application est une donnée dynamique, l'ajout d'une vue de texte statique à la vue de navigation ci-dessous et l'ajout d'une mise en page externe ne fonctionne pas. La approch correcte pour résoudre le problème est décrit ci-dessous lien:

NavigationView with Foother

idée principale est Puting deux NavigationView dans un NavigationView: Un des articles de navigation normale et seconde pour les articles foother.

<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> 
1

J'ai le même problème ..

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");