我如何将ImageView的高度与其父级的wrap_content相匹配? (RecyclerView项)

问题描述 投票:0回答:1

我有一个带有RecyclerViewLinearLayout项目。 LinearLayout的高度设置为wrap_content。在LinearLayout里面,我想有一个与ImageView高度匹配的三角形的LinearLayout,以获得类似这样的内容:

enter image description here

LinearLayout的高度固定,我可以将其设置为XML:

 <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:scaleType="fitXY"
        android:adjustViewBounds="true"/>

为了获得高度可变的相同结果,我尝试将ImageView的高度和宽度设置为0,然后在onBindViewHolder内以编程方式对其进行调整:

holder.mLinearLayout.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
int layoutHeight = holder.mLinearLayout.getMeasuredHeight();

holder.mImageView.getLayoutParams().height = layoutHeight;
holder.mImageView.getLayoutParams().width = layoutHeight;

holder.mImageView.requestLayout();

看起来ImageView的大小可以调整,但不显示图像。我在这里错了吗?

android android-recyclerview imageview
1个回答
0
投票

我应该睡一会儿。上面的代码有效。我不小心删除了XML图像源。

© www.soinside.com 2019 - 2024. All rights reserved.