【问题标题】:Have an ImageView with a default height, change height when an image is put into it有一个具有默认高度的 ImageView,将图像放入其中时更改高度
【发布时间】: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


【解决方案1】:

您可以尝试获取当前布局参数并进行更改。

LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) createdView.getLayoutParams();
params.height = LayoutParams.WRAP_CONTENT;
createdView.setLayoutParams(params);

【讨论】:

  • 效果很好,谢谢!
猜你喜欢
  • 2014-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-08
  • 2013-03-04
  • 2021-12-14
相关资源
最近更新 更多