从另一个布局更改 TextView

3
在我的最新Android项目中,我需要从我的MainActivity中使用activity_main布局来设置在R.layout.header(我的NavDrawer的内容,请参见下面的截图)中的TextView。为此,我调用了我的SetUsername()方法,其中包含以下代码以从首选项中获取用户名:
private void SetUsername(){
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    String userNamePref = preferences.getString("username", "DEFAULT");
    //Change the Username in R.layout.header
}

为了设置用户名,我首先执行了以下操作:
TextView username = (TextView)findViewById(R.id.usernameHeader);
username.setText(userNamePref);

这个方法并没有起作用——很明显,因为R.id.usernameHeaderR.layout.header中。

所以我使用了这个方法:

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

这将正确更改我的NavDrawer中的文本,但activity_main布局消失了,所以我在函数底部添加了setContentView(R.layout.activity_main)。一切都按照预期工作,但文本没有改变。

为了更好地理解,我做了一个屏幕截图,这样您就可以看到我想要改变的内容:

NavDrawer

我的布局:

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>
标签:
 <?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="bt-ahaus@justanotherschool.de"
        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>

工具栏:

  <?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
您可以参考此帖子 https://dev59.com/lFwX5IYBdhLWcg3w8TTM#33699825 - OneCricketeer
工作了!谢谢! 你知道任何“刷新”UI的方法吗?因为我通过首选项更改用户名,应用程序需要重新启动才能接受更改。谢谢! - Daniel Kng
1
我想我理解你的问题了,但是我能想到的唯一方法就是重写MainActivity的onResume方法来更新那个值。 - OneCricketeer
2个回答

5

切换ContentView是完全不必要的。

要在标题布局中查找View,需要在该特定布局的实例上调用findViewById

不要像下面这样:

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

您应该执行以下操作:
NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view);
View header = navigationView.getHeaderView(0);
TextView username = (TextView) header.findViewById(R.id.usernameHeader);

0

据我所理解,您需要首先获取DrawerLayout对象,然后运行mDrawerLayout.findViewById(R.id.usernameHeader);但在对DrawerLayout进行findViewById之前,请记得先设置drawerLayout。

您的标题应该在主活动中。 我附上一个参考用的示例xml。

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

在这里你可以看到,所有的视图都属于主活动。在你的情况下,XML文件中的usernameHeader现在已经附加到了你的主活动上。

希望它能正常工作 :)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接