我在相对布局中添加了两个图像视图并得到了一个奇怪的行为。当使用 RelativeLayout.BELOW 并以编程方式添加图像时,最后一个图像将最后一个图像之一缩小到 50%。我的一张图片显示在另一张图片下方。我使用这段代码:
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>