Android 以编程方式将 imageview 的高度设置为 matchparent
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10310550/
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 height of imageview as matchparent programmatically
提问by andro-girl
I need to set the height of an imageviewas matchparentprogramatically.if it is a fixed height i know how to set.
我需要以编程方式将图像视图的高度设置为matchparent。如果它是固定高度,我知道如何设置。
but how can i set it as matchparent?
但我怎样才能将它设置为matchparent?
EDIT:
编辑:
actually height of the parent layout is dynamic.so i need to make the height of the imageview as the height of parent.
实际上父布局的高度是动态的。所以我需要将图像视图的高度设为父布局的高度。
采纳答案by luckyluke
initiate LayoutParams .
启动 LayoutParams 。
assign the parent's width and height and pass it to setLayoutParams method of the imageview
分配父级的宽度和高度并将其传递给 imageview 的 setLayoutParams 方法
回答by C. Leung
imageView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
回答by Luke Allison
imageView.getLayoutParams().height= ViewGroup.LayoutParams.MATCH_PARENT;
回答by xandy
imageView.setLayoutParams
(new ViewGroup.MarginLayoutParams
(width, ViewGroup.LayoutParams.MATCH_PARENT));
The Type of layout params depends on the parent view group. If you put the wrong one it will cause exception.
布局参数的类型取决于父视图组。如果你放错了它会导致异常。
回答by Muhamed Riyas M
For Kotlin Users
对于 Kotlin 用户
val params = mImageView?.layoutParams as FrameLayout.LayoutParams
params.width = FrameLayout.LayoutParams.MATCH_PARENT
params.height = FrameLayout.LayoutParams.MATCH_PARENT
mImageView?.layoutParams = params
Here I used FrameLayout.LayoutParams
since my views( ImageView
) parent is FrameLayout
在这里我使用,FrameLayout.LayoutParams
因为我的 views( ImageView
) 父母是FrameLayout
回答by Andrea De Simone
I had same issue. Resolved by firstly setting :
我有同样的问题。通过首先设置解决:
imageView.setMinHeight(0);
imageView.setMinimumHeight(0);
And then :
进而 :
imageView.getLayoutParams().height= ViewGroup.LayoutParams.MATCH_PARENT;
setMinHeight is defined by ImageView, while setMinimumHeight is defined by View. According to the docs, the greater of the two values is used, so both must be set.
setMinHeight 由 ImageView 定义,而 setMinimumHeight 由 View 定义。根据文档,使用两个值中的较大值,因此必须设置两者。
回答by Andrea De Simone
You can try this incase you would like to match parent. The dimensions arrangement is width and height inorder
如果您想匹配父母,您可以试试这个。尺寸排列是宽高有序
web = new WebView(this);
web.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
回答by Andrea De Simone
You can use the MATCH_PARENT constant or its numeric value -1.
您可以使用 MATCH_PARENT 常量或其数值 -1。