How to change width and height of existing layout

2019-08-07 13:49发布

问题:

Please consider ImageView inside RelativeLayout it has some specific layout params for RelativeLayout. I need to change the width and height of my ImageView programatically according to the screen size.

How can I save all layout parameters but only change width and height

Following code creates empty layout params but I need to save my old params and change only height and width.

   imageViewArticleImage.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, mScreenSize.y / 3));

How can I achieve this ?

回答1:

Use getLayoutParams to get the current layout params. Change the width and height and go from there. IF the view is already visible, call requestLayout to force a new layout pass.