【问题标题】:how to set layout_span, layout_margin dynamically in code in android如何在android的代码中动态设置layout_span、layout_margin
【发布时间】:2011-01-14 09:16:45
【问题描述】:

如何在android代码中动态设置layout_span、layout_margin

【问题讨论】:

标签: android-layout


【解决方案1】:

您可以使用TableRow.LayoutParams 实例设置 layout_margin 给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++);

【讨论】:

    猜你喜欢
    • 2011-06-05
    • 2012-10-01
    • 1970-01-01
    • 2011-06-06
    • 2012-07-01
    • 2012-06-19
    • 2013-06-08
    • 1970-01-01
    • 2011-08-12
    相关资源
    最近更新 更多