我想在android中设置元素的填充,以便在左,右和底部填充中占3.2%,我已经做过一些研究,但是我没有得到任何有趣的东西,这是我尝试过的。
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
FrameLayout rel = (FrameLayout) findViewById(R.id.tabcontent);
double width_pas= width*3.2;
double height_pas=height*3.2;
rel.setPadding(width_pas,0,width_pas,height_pas);
但是我在setpadding中遇到错误,它仅接受整数,但是我需要将边距设置为3.2
希望您能够帮助我。
最佳答案
三件事:
如果要计算宽度/高度的3.2%,则应将特定值乘以0.032
而不是3.2
。
您不能设置浮动填充(请参见Adem's注释)
如果您使用ints
而不是floats/doubles
,您将得到3%
填充而不是3.2%
,这是不正确的。这取决于您要进行的操作。如果您以这种方式计算填充:
int width_pas = Math.round(width * 0.032f);
int height_pas = Math.round(height * 0.032f);
您最终将获得尽可能精确的高度/宽度的3.2%百分比。