set height of imageview as matchparent programmatically

丶灬走出姿态 提交于 2020-02-17 00:01:28

问题


I need to set the height of an imageview as matchparent programatically.if it is a fixed height i know how to set.

but how can i set it as 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.


回答1:


initiate LayoutParams .

assign the parent's width and height and pass it to setLayoutParams method of the imageview




回答2:


imageView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));



回答3:


imageView.getLayoutParams().height= ViewGroup.LayoutParams.MATCH_PARENT;



回答4:


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.




回答5:


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));



回答6:


For Kotlin Users

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




回答7:


You can use the MATCH_PARENT constant or its numeric value -1.




回答8:


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.



来源:https://stackoverflow.com/questions/10310550/set-height-of-imageview-as-matchparent-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!