android - 如何将 ImageView 高度与其父级 wrap_content 匹配?(回收站查看项目)
问题描述
我有一个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 中删除了图像源。上面的代码现在显示了一个图像,但它仍然不能正常工作。
要获得正确的高度,您必须替换测量参数:
holder.mLinearLayout.measure(
View.MeasureSpec.makeMeasureSpec(Resources.getSystem().getDisplayMetrics().widthPixels, View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)
);
我不确定这是否是最好的解决方案,但它确实有效。
推荐阅读
- python - 星体太阳计算
- php - wordpress 客户仪表板中的编辑地址问题
- lua - 如何阻止 Lua 脚本运行
- python - 'C:\Users\name' 在 Jupyter 笔记本中未被识别为内部或外部命令、可运行程序或批处理文件
- python-3.6 - pycuda 安装中的警告
- regex - 将正则表达式 (1 + 01)∗ (0 + 10)+ 转换为 NFA
- json - 忽略杰克逊 JSON 解析器中的字段?
- laravel - 将 sendgrid 的事件 webhook 路由到 laravel 中的控制器
- flutter - ChangeNotifier 不更新消费者
- uart - 如何使用 bsl 擦除 msp430f2619 闪存?