Search code examples
androidpercentagemargins

How to set margins as percentage of parent layout's size?


I wanna set the margins as percentage.

like this..

ViewGroup.MarginLayoutParams margin = new ViewGroup.MarginLayoutParams(iv_Dot.getLayoutParams());
margin.setMargins(30%,  20%,  0,  0);

But, setMargins method is to support only int type.

How can i set margins as percentage?.

If, I get parent size. and then, calculate percentage. I can solve this problem.

Is there another way?


Solution

  • Other than setting component sizes as percentage of parent, I do not think ther is another way.