2个Imageview的大小相同,一个是wrap_content

时间:2016-09-01 10:11:12

标签: android layout imageview size

我正在寻找一种方法来获得相同大小的两个图像视图。

Imageview A是:width: match_parent height: wrap_content scaleType: centerInside adjustViewBounds: true

Imageview 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);
                }
            });

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

由于

2 个答案:

答案 0 :(得分:0)

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);

答案 1 :(得分:0)

你试过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>