相対レイアウトで 2 つのイメージビューを追加しましたが、奇妙な動作が発生します。RelativeLayout.BELOW を使用してプログラムで画像を追加すると、最後の画像は最後の画像の 1 つを 50% に縮小します。私の画像の 1 つが他の画像の下に表示されます。私はこのコードを使用します:
RelativeLayout layout = new RelativeLayout(getActivity());
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layout.setLayoutParams(params);
layout.setId(10001);
params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_HORIZONTAL);
_imageView = new ImageView(getActivity());
_imageView.setId(10002);
_imageView.setImageResource(_resource);
layout.addView(_imageView, params);
params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, _imageView.getId());
params.addRule(RelativeLayout.ALIGN_LEFT, _imageView.getId());
ImageView shadowImage = new ImageView(getActivity());
shadowImage.setId(10003);
shadowImage.setImageResource(R.drawable.card_shadow);
shadowImage.setAdjustViewBounds(true);
layout.addView(shadowImage, params);
しかし、プログラムで画像を追加する代わりに xml を使用すると、画像がフルサイズで取得されます。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/cardShadow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/cardView"
android:layout_alignLeft="@drawable/card_01"
android:src="@drawable/card_shadow"
android:contentDescription=""/>
<ImageView
android:id="@+id/cardView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="@drawable/card_01" />
</RelativeLayout>