Android动态设置margins


拿TableLayout举例

TableRow.LayoutParams p = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT);  

p.setMargins(2, 2, 2, 2); //left,top,right,bottom

TableRow tr1 = (TableRow) findViewById(R.id.tr1);

tr1.addView(yourview,p);

----------------------------------------

每一种Layout都有自己的LayoutParams

再用Layout添加view的时候 只需要调用addView(view,layoutParams);这个重载方法就可以动态设定在xml中设置的很多属性

相关内容