0

私は、すべてのアクティビティの上部にヘッダーを持つアプリを作成しています。これまでのところ、私はそれを行ってきました:

すべてのページの 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>
4

2 に答える 2

1

ImageView の場合と同様に、header.xml で TextView に ID を指定します。そして、各アクティビティの onCreate() メソッドでテキストを設定できます。

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

id 属性を使用した TextView を含む 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: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>
于 2012-05-04T12:52:28.173 に答える
0

この回答は、使用する別のアプローチになる可能性があります。また、すべての xml にヘッダーを追加する必要はありません。また、各アクティビティでヘッダーのテキストビューを作成する必要はありません。

于 2012-05-04T12:56:10.353 に答える