1

別の b.xml レイアウトに含まれる a.xml レイアウトにあるビューにアクセスする必要があります。たとえば、これは a.xml です。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <Button
            android:id="@+id/xyz"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="XYZ" />
</RelativeLayout>

そして、b.xmlで

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <include
        android:id="@+id/a_layout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        layout="@layout/a" />

    <TextView
        android:id="@+id/label"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/xyz"
        android:text="Show me below xyz" />
</RelativeLayout>

Javaで行う場合はsetContentView()の後に行う必要があり、TextViewの「ラベル」のLayoutParamsを設定しても効果がないため、xmlコードで行う必要があります。

私が何を尋ねようとしているのか、誰もが理解していると思います。良い返事を待っています。

皆さんありがとう。

右側の画像は私が達成しようとしているもので、左側の画像は現在のコードで得ているものです。

これは、現在のxmlコードで取得しているものです これが私がやろうとしていることです

4

2 に答える 2

3

b.xml で、以下を修正する必要があります。

android:layout_below="@id/xyz"

android:layout_below="@id/a_layout"

そして、それをコードで使用できます (ここでは onCreate に配置します)。

setContentView(R.layout.b);    
((Button)findViewById(R.id.xyz)).setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            ((TextView)findViewById(R.id.label)).setText("clicked on XYZ button");
        }
    });
于 2012-04-10T08:13:54.237 に答える
0

問題は、含まれているレイアウトにアクセスするViewことではなく、このレイアウトを「オーバーラップ」させることができないという事実にあります。説明させてください: ボタンの下にいくつかのビューを追加してから、ボタンのすぐ下にa.xmlいくつかのビューを配置しようとすると、 からビューがオーバーラップしますが、これは Android では実装されていません (まだ? )。だから、あなたができる唯一のことは、@HoàngToảnが提案したように置くことです.b.xmlb.xmla.xmlandroid:layout_below="@id/a_layout"

PS このa+bレイアウトの組み合わせで同じ動作が見られる場合があります。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <Button
            android:id="@+id/xyz"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="XYZ" />
    </RelativeLayout>

    <TextView
        android:id="@+id/label"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/xyz"
        android:text="Show me below xyz" />
</RelativeLayout>
于 2012-04-10T08:35:33.297 に答える