android-从另一个布局更改TextView

android-从另一个布局更改TextView,第1张

概述对于我在Android中的最新项目,我需要从MainActivity设置R.layout.header(NavDrawer的内容,请参见下面的屏幕截图)中的TextView,它使用activity_main布局.为此,我调用了SetUsername()方法,该方法包含以下代码以从“首选项”中获取用户名:privatevoidSetUsername(){SharedPr

对于我在Android中的最新项目,我需要从MainActivity设置R.layout.header(NavDrawer的内容,请参见下面的屏幕截图)中的TextVIEw,它使用activity_main布局.

为此,我调用了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.usernameheader在R.layout.header中.

所以我用了这个:

setContentVIEw(R.layout.header);TextVIEw username = (TextVIEw)findVIEwByID(R.ID.usernameheader);username.setText(usernamePref);

哪个可以正确更改NavDrawer中的Text,但是activty_main布局不见了,所以我在函数底部添加了setContentVIEw(R.layout.activity_main).一切都按预期进行,但文本没有更改.

为了获得更好的主意,我制作了一个屏幕截图,以便您可以看到我要更改的内容:


我的版式:

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>

解决方法:

完全不需要切换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);
总结

以上是内存溢出为你收集整理的android-从另一个布局更改TextView全部内容,希望文章能够帮你解决android-从另一个布局更改TextView所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1090681.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-27
下一篇2022-05-27

发表评论

登录后才能评论

评论列表(0条)

    保存