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:
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>
Sie auf dieses Thema beziehen http://stackoverflow.com/a/33699825/2308683 –
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! –
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. –