【问题标题】:Changing the value of a TextView更改 TextView 的值
【发布时间】:2012-05-14 00:15:37
【问题描述】:

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

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

这是我迄今为止一直在做的事情:

选择活动,

带有布局的chooseact.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>

【问题讨论】:

标签: android textview


【解决方案1】:

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

TextView headerText = (TextView) findById(R.id.header_text);
headerText.setText("The text that should show up");

header.xml 与 TextView 使用 id 属性:

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2014-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多