如何在Android代码中动态设置layout_span,layout_margin [英] how to set layout_span, layout_margin dynamically in code in android
本文介绍了如何在Android代码中动态设置layout_span,layout_margin的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在android代码中动态设置layout_span,layout_margin
how to set layout_span, layout_margin dynamically in code in android
推荐答案
您可以使用给TableRow.addView()
的TableRow.LayoutParams
实例来设置layout_margin:
You can set layout_margin with a TableRow.LayoutParams
instance given to TableRow.addView()
:
TableLayout newTable = new TableLayout(DisplayContext);
int rowIndex = 0;
// First Row (3 buttons)
TableRow newRow1 = new TableRow(DisplayContext);
newRow1.addView(new Button(DisplayContext), 0);
newRow1.addView(new Button(DisplayContext), 1);
newRow1.addView(new Button(DisplayContext), 2);
newTable.addView(newRow1, rowIndex++);
// Second Row (2 buttons, last one spans 2 columns)
TableRow newRow2 = new TableRow(DisplayContext);
newRow2.addView(new Button(DisplayContext), 0);
Button newButton = new Button(DisplayContext);
// Create the layout to span two columns
TableRow.LayoutParams params = new TableRow.LayoutParams();
params.span = 2;
newRow2.addView(newButton, 1, params);
newTable.addView(newRow2, rowIndex++);
这篇关于如何在Android代码中动态设置layout_span,layout_margin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文