我有一个带有RecyclerView
的LinearLayout
项目。 LinearLayout
的高度设置为wrap_content
。在LinearLayout
里面,我想有一个与ImageView
高度匹配的三角形的LinearLayout
,以获得类似这样的内容:
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
的大小可以调整,但不显示图像。我在这里错了吗?
我应该睡一会儿。上面的代码有效。我不小心删除了XML图像源。