Android 你如何为 ImageView 设置布局参数()?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2965662/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:05:14  来源:igfitidea点击:

How do you setLayoutParams() for an ImageView?

androidlayoutimageview

提问by DeadTime

I want to set the LayoutParamsfor an ImageViewbut cant seem to find out the proper way to do it.

我想设置LayoutParamsfor ,ImageView但似乎无法找到正确的方法。

I can only find documentation in the API for the various ViewGroups, but not an ImageView. Yet the ImageViewseems to have this functionality.

我只能对各种API中找到文档ViewGroups,而不是一个ImageView。然而ImageView似乎有这个功能。

This code doesn't work...

此代码不起作用...

myImageView.setLayoutParams(new ImageView.LayoutParams(30,30));

How do I do it?

我该怎么做?

回答by Steve Haley

You need to set the LayoutParams of the ViewGroup the ImageView is sitting in. For example if your ImageView is inside a LinearLayout, then you create a

您需要设置 ImageView 所在的 ViewGroup 的 LayoutParams。例如,如果您的 ImageView 在 LinearLayout 内,那么您创建一个

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(30, 30);
yourImageView.setLayoutParams(layoutParams);

This is because it's the parent of the View that needs to know what size to allocate to the View.

这是因为它是 View 的父级,需要知道分配给 View 的大小。

回答by Bogdan

Old thread but I had the same problem now. If anyone encounters this he'll probably find this answer:

旧线程,但我现在遇到了同样的问题。如果有人遇到这个,他可能会找到这个答案:

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(30, 30);
yourImageView.setLayoutParams(layoutParams);

This will work only if you add the ImageView as a subView to a LinearLayout. If you add it to a RelativeLayout you will need to call:

仅当您将 ImageView 作为子视图添加到 LinearLayout 时,这才有效。如果将其添加到 RelativeLayout 中,则需要调用:

RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(30, 30);
yourImageView.setLayoutParams(layoutParams);

回答by Karl Giesing

If you're changing the layout of an existing ImageView, you should be able to simply get the current LayoutParams, change the width/height, and set it back:

如果您要更改现有 ImageView 的布局,您应该能够简单地获取当前的 LayoutParams,更改宽度/高度,然后将其重新设置:

android.view.ViewGroup.LayoutParams layoutParams = myImageView.getLayoutParams();
layoutParams.width = 30;
layoutParams.height = 30;
myImageView.setLayoutParams(layoutParams);

I don't know if that's your goal, but if it is, this is probably the easiest solution.

我不知道这是否是您的目标,但如果是,这可能是最简单的解决方案。

回答by Codeversed

An ImageView gets setLayoutParams from View which uses ViewGroup.LayoutParams. If you use that, it will crash in most cases so you should use getLayoutParams() which is in View.class. This will inherit the parent View of the ImageView and will work always. You can confirm this here: ImageView extends view

ImageView 从使用 ViewGroup.LayoutParams 的 View 获取 setLayoutParams。如果你使用它,在大多数情况下它会崩溃,所以你应该使用 View.class 中的 getLayoutParams()。这将继承 ImageView 的父视图并且将始终有效。您可以在这里确认:ImageView extends view

Assuming you have an ImageView defined as 'image_view' and the width/height int defined as 'thumb_size'

假设您将 ImageView 定义为“ image_view”,并将宽度/高度 int 定义为“thumb_size”

The best way to do this:

最好的方法是

ViewGroup.LayoutParams iv_params_b = image_view.getLayoutParams();
iv_params_b.height = thumb_size;
iv_params_b.width = thumb_size;
image_view.setLayoutParams(iv_params_b);