2 个图像视图的大小相同,一个是 wrap_content

Same size for 2 Imageviews, one is wrap_content

我正在寻找使我的两个图像视图大小相同的方法。

图像视图 A 是:width: match_parent height: wrap_content scaleType: centerInside adjustViewBounds: true

图像视图 B 是: width: match_parent height: wrap_content

而ImageView B在Imageview A之上。但是,A的高度是由他的内容决定的。所以我希望我的 ImageView B 的大小与 A 的大小相同。

我试过了:(在我的 RecyclerView 适配器中)。

final ImageView v2 = holder.miniatureVideo;
            final ImageView shadow2 = holder.shadow;
            v2.post(new Runnable() {
                @Override
                public void run() {
                    shadow2.getLayoutParams().height = v2.getHeight();
                    ViewGroup.LayoutParams params = (ViewGroup.LayoutParams) shadow2.getLayoutParams();
                    params.height = v2.getHeight();
                    shadow2.setLayoutParams(params);
                }
            });

但是在正确调整大小之前会有一点延迟。有时我必须滚动才能调整大小。

谢谢

int finalHeight, finalWidth;
final ImageView imageView1= (ImageView)findViewById(R.id.scaled_image);
ViewTreeObserver vto = imageView1.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
     public boolean onPreDraw() {
        imageView1.getViewTreeObserver().removeOnPreDrawListener(this);
        finalHeight = imageView1.getMeasuredHeight();
        finalWidth = imageView1.getMeasuredWidth();
        return true;
     }
});

先获取上面ImageOne的高度..然后如下设置ImageTwo

 android.view.ViewGroup.LayoutParams layoutParams = imageView2.getLayoutParams();
 layoutParams.width = finalWidth;
 layoutParams.height = finalHeight ;
 imageView2.setLayoutParams(layoutParams);

你试过了吗RelativeLayout

像这样:

  <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

        <ImageView
                android:id="@+id/image_a"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:adjustViewBounds="true"
                android:scaleType="centerInside"
                android:src="@drawable/imageA"/>

        <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignTop="@id/image_a"
                android:layout_alignBottom="@id/image_a"
                android:src="@drawable/imageB"/>

    </RelativeLayout>