7

私はiOSのバックグラウンドから来ました。何らかの理由で、ビューを別のビューに追加する方法がわかりません。

ImageView次のようにプログラムで作成している 2 つの があります。

ImageView imageView;
ImageView imageHolder;

今、私はこのようなことをしたい:

imageHolder.addView(imageView);

どうすればこれを達成できますか? グーグルをたくさんしましたが、役に立ちませんでした。

4

1 に答える 1

12

pskink が言ったように、 ViewGroupである何かにプログラムでのみビューを追加できます。LinearLayoutたとえば、次のように に追加できます。

LinearLayout layout = (LinearLayout)findViewById(R.id.linear_layout);
layout.addView(new EditText(context));

ただし、それはおそらくあなたのシナリオには役立ちません。画像を別の画像の上に配置するには、 Relative Layoutを使用できます。通常、これは XML レイアウト ファイルで設定します。

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

    <ImageView
        android:id="@+id/backgroundImage"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <ImageView
        android:id="@+id/foregroundImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/backgroundImage"
        android:layout_alignLeft="@id/backgroundImage" />

</RelativeLayout>

事前に画像がどうなるかわからない場合は、コードで画像を指定できます。

((ImageView)findViewById(R.id.backgroundImage)).setImageResource(R.drawable.someBackgroundImage);
于 2013-10-28T19:43:40.283 に答える