2016-05-04 6 views
0

Ich folgte einem Tutorial um http://www.android4devs.com/2015/06/navigation-view-material-design-support.html und habe es in meinem Projekt implementiert, aber ich möchte den Text in der Kopfzeile dynamisch ändern, wie ein Benutzer anmeldet, aber meine Referenz auf die Textansicht immer als zurück null, ich habe alles probiert.wie Ansichten in Navigationsseitenkopf steuern

heren mein header.xml Code

<?xml version="1.0" encoding="utf-8"?> 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="220dp" 
xmlns:tools="http://schemas.android.com/tools" 
android:background="@drawable/background_material" 
android:orientation="vertical" 
android:id="@+id/header_relative_layout" 
tools:context=".NavMainActivity"> 

<de.hdodenhof.circleimageview.CircleImageView 
xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/profile_image" 
    android:layout_width="76dp" 
    android:layout_height="76dp" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:layout_centerVertical="true" 
    android:layout_marginLeft="24dp" 
    android:layout_marginStart="24dp" 
    android:src="@drawable/profile" 
    app:border_color="#FF000000" /> 

<TextView 
    android:id="@+id/username" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/schoolname" 
    android:layout_alignLeft="@+id/profile_image" 
    android:layout_alignStart="@+id/profile_image" 
    android:gravity="left" 
    android:paddingBottom="4dp" 
    android:text="Staff Name" 
    android:textColor="#FFF" 
    android:textSize="16sp" 
    android:textStyle="bold" /> 

<TextView 
    android:id="@+id/schoolname" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/username" 
    android:layout_alignParentBottom="true" 
    android:layout_alignStart="@+id/username" 
    android:layout_marginBottom="8dp" 
    android:gravity="left" 
    android:text="Secondary School" 
    android:textColor="#fff" 
    android:textSize="18sp" /> 

</RelativeLayout> 

und herer meine nav_activity_main.xml

<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/drawer" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:fitsSystemWindows="true" 
tools:context=".NavMainActivity"> 

<LinearLayout 
    android:id="@+id/main_view" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    > 

    <include 
     android:id="@+id/toolbar" 
     layout="@layout/tool_bar" /> 

    <FrameLayout 
     android:id="@+id/frame" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

    </FrameLayout> 

</LinearLayout> 

<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/header" 
    app:menu="@menu/drawer" /> 
</android.support.v4.widget.DrawerLayout> 

here meine Tätigkeit

setContentView(R.layout.nav_activity_main); 
RelativeLayout headerLayout = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.header, null); 
TextView staff = (TextView) headerLayout.findViewById(R.id.username); 
staff.setText("it worked"); 

ive versucht, alle Lösungen keine zu funktionieren scheint, pls Was mache ich falsch.

Antwort

0

Um mit Elementen in der Kopfzeile der Navigationsleiste zu kommunizieren, müssen Sie zuerst das Layout wie diese:

//Initializing NavigationView 
navigationView = (NavigationView) findViewById(R.id.navigation_view); 

if(navigationView!=null) { 

     // Get the header layout 
     headerLayout = navigationView.getHeaderView(0); 
} 

danach Sie die folgenden Ansichten, indem Sie manipulieren können:

TextView staff = (TextView) headerLayout.findViewById(R.id.username); 
staff.setText("it worked"); 
+0

danke bro es hat funktioniert – Light

+1

bro.setText ("Es hat funktioniert"); – Light