Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 更改TextView的值_Android_Textview - Fatal编程技术网

Android 更改TextView的值

Android 更改TextView的值,android,textview,Android,Textview,我正在创建一个应用程序,该应用程序在每个活动的顶部都有标题,目前为止,我一直在这样做: 为每个页面创建header.xml。但我认为必须有一种更有效的方法。 我可以拥有一个常量header.xml,然后在onCreate方法中更改文本的值吗 到目前为止,我一直在这样做: 选择活动 使用布局选择seact.xml <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout

我正在创建一个应用程序,该应用程序在每个活动的顶部都有标题,目前为止,我一直在这样做:

为每个页面创建header.xml。但我认为必须有一种更有效的方法。
我可以拥有一个常量header.xml,然后在onCreate方法中更改文本的值吗

到目前为止,我一直在这样做:

选择活动

使用布局选择seact.xml

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/white">

   <include layout="@layout/selectheader" />
   <include layout="@layout/redcell" />

   <ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
   />


</TableLayout>

其中包含header.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >



    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:src="@drawable/headerrect" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:gravity="center"
        android:text="@string/leagues"
        android:textColor="@color/white"
        android:textSize="25dp"
        android:textStyle="bold"/>


</RelativeLayout>

为header.xml中的TextView指定一个id,就像对ImageView所做的那样。然后在每个活动的onCreate()方法中,可以设置文本

TextView headerText = (TextView) findById(R.id.header_text);
headerText.setText("The text that should show up");
header.xml,带有使用id属性的TextView:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >



    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:src="@drawable/headerrect" />

    <TextView
        android:id="@+id/header_text"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:gravity="center"
        android:text="@string/leagues"
        android:textColor="@color/white"
        android:textSize="25dp"
        android:textStyle="bold"/>

</RelativeLayout>


可以是另一种使用的方法..并且您不需要在所有xml中添加标题..也不需要在每个活动中创建标题文本视图。

那么您的问题是什么?他想要一种更有效的方法来更改文本视图而不创建新字符串,对吗?基于此,您做得很好,只是再检查一下