2016-06-11 16 views
1

Für mein aktuelles Projekt in Android, ich brauche eine TextView zu setzen, die in R.layout.header ist (für meine NavDrawer Der Inhalt, siehe Screenshot unten) von meinem MainActivity, die die activity_main Layout verwendet.ändern Textview von einem anderen Layout-

zu tun, dass ich meine SetUsername() Methode aufrufen, die den folgenden Code enthält den Benutzernamen von den Einstellungen zu erhalten:

private void SetUsername(){ 
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); 
    String userNamePref = preferences.getString("username", "DEFAULT"); 
    //Change the Username in R.layout.header 
} 

den Benutzernamen gesetzt i

TextView username = (TextView)findViewById(R.id.usernameHeader); 
username.setText(userNamePref); 

die zuerst tat nicht funktioniert - offensichtlich, weil R.id.usernameHeader ist in der R.layout.header.

So habe ich dies:

setContentView(R.layout.header); 
TextView username = (TextView)findViewById(R.id.usernameHeader); 
username.setText(userNamePref); 

die den Text in meinem NavDrawer korrekt geändert, aber die activty_main Layout war verschwunden, so fügte ich setContentView(R.layout.activity_main) am unteren Rand meiner Funktion. Und alles funktioniert wie vorgesehen, aber der Text hat sich nicht geändert.

Um eine bessere Vorstellung ich einen Screenshot gemacht, so dass Sie genau sehen können, was ich ändern wollen: NavDrawer

Meine Layouts:

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/drawer" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context=".MainActivity"> 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/bg_screen3"> 
    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" 
     android:gravity="center_horizontal" 
     android:orientation="vertical" 
     android:id="@+id/linearLayout"> 

     <ImageView 
      android:layout_width="@dimen/img_width_height" 
      android:layout_height="@dimen/img_width_height" 
      android:src="@drawable/ic_assessment_white_36dp"/> 

     <TextView 
      android:id="@+id/stundenplanText" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/slide_2_title" 
      android:textColor="@android:color/white" 
      android:textSize="@dimen/slide_title" 
      android:textStyle="bold" /> 

     <Spinner 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/class_spinner" 
      android:layout_below="@+id/linearLayout" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentStart="true" /> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/button_start" 
      android:id="@+id/okay_button" 
      android:elevation="5dp" 
      android:layout_below="@+id/linearLayout" 
      android:layout_centerHorizontal="true" 
      android:layout_marginTop="39dp"> 
     </Button> 
    </LinearLayout> 
    <include 
     android:id="@+id/tool_bar" 
     layout="@layout/toolbar"> 
    </include> 
</RelativeLayout> 
    <android.support.design.widget.NavigationView 
     android:id="@+id/navigation_view" 
     android:layout_height="match_parent" 
     android:layout_width="wrap_content" 
     android:layout_gravity="start" 
     app:headerLayout="@layout/header" 
     app:menu="@menu/drawer" 
     /> 
</android.support.v4.widget.DrawerLayout> 

header:

<?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="190dp" 
    android:background="@color/bg_screen3" 
    android:orientation="vertical" 
    android:id="@+id/headerRelative"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="BT-Ahaus" 
     android:textSize="14sp" 
     android:textColor="#FFF" 
     android:textStyle="bold" 
     android:gravity="left" 
     android:paddingBottom="4dp" 
     android:id="@+id/usernameHeader" 
     android:layout_above="@+id/email" 
     android:layout_alignParentStart="true" 
     android:layout_marginStart="30dp" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="[email protected]" 
     android:id="@+id/email" 
     android:gravity="left" 
     android:layout_marginBottom="8dp" 
     android:textSize="14sp" 
     android:textColor="#fff" 
     android:layout_alignParentBottom="true" 
     android:layout_alignLeft="@+id/usernameHeader" 
     android:layout_alignStart="@+id/usernameHeader" /> 

    <ImageView 
     android:id="@+id/profile_image" 
     android:layout_width="150dp" 
     android:layout_height="100dp" 
     android:src="@drawable/bt_ahaus" 
     android:layout_centerVertical="true" 
     android:layout_alignEnd="@+id/email" /> 

</RelativeLayout> 

Symbolleiste:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@color/bg_screen3" 
    android:theme="@style/ThemeOverlay.AppCompat.Dark" 
    android:elevation="4dp"> 

</android.support.v7.widget.Toolbar> 
+1

Sie auf dieses Thema beziehen http://stackoverflow.com/a/33699825/2308683 –

+0

Arbeitete! Vielen Dank! Kennen Sie die Benutzeroberfläche? Da ich den Benutzernamen durch die Einstellungen ändere, muss die App neu starten, um die Änderungen zu akzeptieren. Vielen Dank! –

+1

Ich glaube, ich verstehe, was Sie fragen, aber der einzige Weg, an den ich denken kann, ist das Überschreiben von 'onResume' der MainActivity, um diesen Wert zu aktualisieren. –

Antwort

2

die ContentView Schalt ist völlig unnötig.

Um ein View auf dem Layout Ihrer Kopfzeile zu finden, müssen Sie findViewById auf der bestimmten Layout-Instanz aufrufen.

Statt der folgenden Möglichkeiten:

TextView username = (TextView)findViewById(R.id.usernameHeader); 

Sie sollten wie folgt vorgehen:

NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view); 
View header = navigationView.getHeaderView(0); 
TextView username = (TextView) header.findViewById(R.id.usernameHeader); 
0

Wie ich in der Lage bin zu verstehen, müssen Sie zuerst das Objekt DrawerLayout, und dann laufen mDrawerLayout.findViewById(R.id.usernameHeader); Denken Sie jedoch daran, drawerLayout zu konfigurieren, bevor Sie findViewById in DrawerLayout ausführen.

Ihre Kopfzeile sollte in Ihrer Haupttätigkeit sein. Ich füge eine Probe xml als Referenz an.

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

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

     <LinearLayout 
      android:id="@+id/toolbar_home" 
      android:layout_width="match_parent" 
      android:layout_height="@dimen/toolbar_height" 
      android:background="@color/red"> 

      <include 
       layout="@layout/toolbar" 
       android:layout_width="match_parent" 
       android:layout_height="@dimen/toolbar_height" 
       android:elevation="5dp" /> 
     </LinearLayout> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_below="@+id/toolbar_home"> 
     </RelativeLayout> 
    </RelativeLayout> 

    <LinearLayout 
     android:layout_width="260dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:background="#ffffff" 
     android:orientation="vertical" 
     android:scrollbars="none"> 

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

      <ImageView 
       android:id="@+id/user_banner" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:scaleType="fitXY" /> 

      <ImageView 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:background="#AA000000" /> 

      <LinearLayout 
       android:id="@+id/topView" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:gravity="center" 
       android:orientation="horizontal"> 

       <LinearLayout 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:orientation="vertical"> 

        <fandooo.com.fandooo.wrapper.CircleImageView 
         android:id="@+id/circularView" 
         android:layout_width="100dp" 
         android:layout_height="100dp" 
         android:layout_below="@+id/imageView" 
         android:layout_centerHorizontal="true" 
         android:src="@drawable/profile_photo_silhouette_48dp" /> 

        <TextView 
         android:id="@+id/text_fullname" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_gravity="center" 
         android:gravity="center" 
         android:paddingTop="5dp" 
         android:text="----" 
         android:textColor="#ffffff" 
         android:textSize="15sp" /> 

        <TextView 
         android:id="@+id/text_fanscore" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_gravity="center" 
         android:gravity="center" 
         android:paddingTop="5dp" 
         android:text="Fan Score 0" 
         android:textColor="#ffffff" 
         android:textSize="15sp" /> 

       </LinearLayout> 

      </LinearLayout> 

     </RelativeLayout> 

     <android.support.v7.widget.RecyclerView 
      android:id="@+id/menuItemList" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_below="@id/topView" 
      android:layout_gravity="left" 
      android:scrollbars="none" /> 
    </LinearLayout> 


</android.support.v4.widget.DrawerLayout> 

Hier können Sie sehen, alle Ansichten gehört zur Haupttätigkeit. In Ihrem Fall wurde die XML mit BenutzernameHeader jetzt an Ihre Hauptaktivität angehängt.

Hoffe, es funktioniert :)

Verwandte Themen