Java 以编程方式设置 ImageView 的宽度和高度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3144940/
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
Set ImageView width and height programmatically?
提问by praveenb
How can I set an ImageView
's width and height programmatically?
如何以ImageView
编程方式设置 a的宽度和高度?
采纳答案by Hakem Zaied
It may be too late but for the sake of others who have the same problem, to set the height of the ImageView:
可能为时已晚,但为了其他有同样问题的人,设置 ImageView 的高度:
image_view.getLayoutParams().height = 20;
Hope this helps.
希望这可以帮助。
Important.If you're setting the height after the layout has already been 'laid out', make sure you also call:
重要的。如果您在布局已经“布局”之后设置高度,请确保您还调用:
image_view.requestLayout()
回答by InvulgoSoft
If your image view is dynamic, the answer containing getLayout will fail with null-exception.
如果您的图像视图是动态的,则包含 getLayout 的答案将因空异常而失败。
In that case the correct way is:
在这种情况下,正确的方法是:
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(100, 100);
iv.setLayoutParams(layoutParams);
回答by duggu
This simple way to do your task:
这种完成任务的简单方法:
setContentView(R.id.main);
ImageView iv = (ImageView) findViewById(R.id.left);
int width = 60;
int height = 60;
LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(width,height);
iv.setLayoutParams(parms);
and another way if you want to give screen size in height and width then use below code :
和另一种方式,如果你想在高度和宽度上给出屏幕尺寸,然后使用下面的代码:
setContentView(R.id.main);
Display display = getWindowManager().getDefaultDisplay();
ImageView iv = (LinearLayout) findViewById(R.id.left);
int width = display.getWidth(); // ((display.getWidth()*20)/100)
int height = display.getHeight();// ((display.getHeight()*30)/100)
LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(width,height);
iv.setLayoutParams(parms);
hope use full to you.
希望对你充分利用。
回答by Vikas
image_view.getLayoutParams().height
returns height value in pixels. You need to get the screen dimensions first to set the values properly.
以像素为单位返回高度值。您需要先获取屏幕尺寸才能正确设置值。
Display display = context.getWindowManager().getDefaultDisplay();
DisplayMetrics outMetrics = new DisplayMetrics();
display.getMetrics(outMetrics);
int screen_height = outMetrics.heightPixels;
int screen_width = outMetrics.widthPixels;
After you get the screen dimensions, you can set the imageview width&height using proper margins.
获得屏幕尺寸后,您可以使用适当的边距设置图像视图的宽度和高度。
view.getLayoutParams().height = screen_height - 140;
view.getLayoutParams().width = screen_width - 130 ;
回答by Ali Kahoot
The best and easiest way to set a button dynamically is
动态设置按钮的最佳和最简单的方法是
Button index=new Button(this);
int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 45, getResources().getDisplayMetrics());
int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 42, getResources().getDisplayMetrics());
The above height and width are in pixels px. 45 being the height in dpand 42 being the width in dp.
上面的高度和宽度以像素为单位px。45是在高度DP和42是在宽度DP。
index.setLayoutParams(new <Parent>.LayoutParams(width, height));
So, for example, if you've placed your button within a TableRow within a TableLayout, you should have it as TableRow.LayoutParams
因此,例如,如果您已将按钮放置在 TableLayout 内的 TableRow 中,则应将其作为 TableRow.LayoutParams
index.setLayoutParams(new TableRow.LayoutParams(width, height));
回答by Omar
If you need to set your width or height to match_parent
(as big as its parent) or wrap_content
(large enough to fit its own internal content), then ViewGroup.LayoutParams
has this two constants:
如果您需要将宽度或高度设置为match_parent
(与其父项一样大)或wrap_content
(足够大以适应其内部内容),则ViewGroup.LayoutParams
具有以下两个常量:
imageView.setLayoutParams(
new ViewGroup.LayoutParams(
// or ViewGroup.LayoutParams.WRAP_CONTENT
ViewGroup.LayoutParams.MATCH_PARENT,
// or ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.MATCH_PARENT ) );
Or you can set them like in Hakem Zaied's answer:
或者您可以像在 Hakem Zaied 的回答中一样设置它们:
imageView.getLayoutParams().width = ViewGroup.LayoutParams.MATCH_PARENT;
//...
回答by Zubair Akber
Simply create a LayoutParams object and assign it to your imageView
只需创建一个 LayoutParams 对象并将其分配给您的 imageView
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(150, 150);
imageView.setLayoutParams(params);
回答by Hiren Patel
I have played this one with pixel
and dp
.
我用pixel
和玩过这个dp
。
private int dimensionInPixel = 200;
How to set by pixel:
如何按像素设置:
view.getLayoutParams().height = dimensionInPixel;
view.getLayoutParams().width = dimensionInPixel;
view.requestLayout();
How to set by dp:
如何通过dp设置:
int dimensionInDp = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dimensionInPixel, getResources().getDisplayMetrics());
view.getLayoutParams().height = dimensionInDp;
view.getLayoutParams().width = dimensionInDp;
view.requestLayout();
Hope this would help you.
希望这会帮助你。
回答by tienanhcntt2
You can set value for all case.
您可以为所有情况设置值。
demoImage.getLayoutParams().height = 150;
demoImage.getLayoutParams().width = 150;
demoImage.setScaleType(ImageView.ScaleType.FIT_XY);
回答by Rohit Mandiwal
In the scenario where the widget size needs to be set programmatically, ensure the below rules.
在需要以编程方式设置小部件大小的场景中,请确保以下规则。
- Set
LayoutParam
for the Layout in which you are adding that view. In my case I am adding toTableRow
so I had to doTableRow.LayoutParams
- Follow this code
LayoutParam
为您添加该视图的布局设置。就我而言,我正在添加,TableRow
所以我必须这样做TableRow.LayoutParams
- 按照这个代码
final float scale = getResources().getDisplayMetrics().density;
int dpWidthInPx = (int) (17 * scale);
int dpHeightInPx = (int) (17 * scale);
final float scale = getResources().getDisplayMetrics().density;
int dpWidthInPx = (int) (17 * scale);
int dpHeightInPx = (int) (17 * scale);
TableRow.LayoutParams deletelayoutParams = new TableRow.LayoutParams(dpWidthInPx, dpHeightInPx);
button.setLayoutParams(deletelayoutParams);
tableRow.addView(button, 1);
TableRow.LayoutParams deletelayoutParams = new TableRow.LayoutParams(dpWidthInPx, dpHeightInPx);
button.setLayoutParams(deletelayoutParams);
tableRow.addView(button, 1);