Margin/Padding in percentage in XML

前端 未结 4 783
Happy的楠姐
Happy的楠姐 2021-01-18 04:47

I am new in android development. I want to set margin & padding in Xml,not in dp or px but in percentage. Is there any way to do this?

4条回答
  •  被撕碎了的回忆
    2021-01-18 05:22

    it is not possible, though You better do it by taking width and height of screen like below in your java code, and

     Display display = getWindowManager().getDefaultDisplay(); 
     int width = display.getWidth();
     int height = display.getHeight();
    

    then calculate your margin from screen size, and set it by

      setmargin(int x) //method of view/layout.
    

    this way you can set margin depending on the screen size not fixed for all screen, and basically it is like setting in percentage but from Java code.

提交回复
热议问题