【发布时间】:2016-01-16 13:18:55
【问题描述】:
所以我正在制作一个包含多张用户选择图片的应用程序。我有预设高度为 300dp 的默认 ImageView。将图像放入 ImageView 后,我希望此高度更改为 wrap_content。我知道这样做的唯一方法是从布局中删除图像,然后使用新的 LayoutParams 重新添加它,但这会打乱我布局中其他视图的顺序。我可以在不移除的情况下更改高度吗?
基本上:
LinearLayout mainLayout = (LinearLayout) findViewById(R.id.mainLayout);
final float scale = getResources().getDisplayMetrics().density;
LinearLayout.LayoutParams mTestImgParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
(int) Math.ceil(scale * 300)
);
final ImageView createdView = new ImageView(this);
mainLayout.addView(createdView, mTestImgParams);
//onLongClick listener, get picture, set the picture into the imageview, etc.
我想改变一下
(int) Math.ceil(scale*300)
到
LinearLayout.LayoutParams.WRAP_CONTENT
无需删除和重新添加 ImageView,并且仅在放置图像之后。请帮忙。
【问题讨论】:
-
在 xml 中你可以使用 android:minHeight=300dp 然后使用高度的环绕内容
标签: android image dynamic imageview height