【问题标题】:How to set padding to columns in table layout dynamically如何动态为表格布局中的列设置填充
【发布时间】:2011-05-31 23:36:13
【问题描述】:

我正在尝试使用动态按钮创建表格布局。我可以通过按钮获得表格布局。 bt 我需要按钮之间的填充。我如何以编程方式获得。

我尝试了以下代码

private void showCowsTblField()
{

    for (int row = 0; row < numberOfRowsInField-1; row++)
    {
        TableRow tableRow = new TableRow(this);  
        tableRow.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT ));
        for (int column = 0; column < numberOfColumnsInField -1; column++)
        {
            blocks[row][column].setLayoutParams(new LayoutParams(  
                    LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); 
            blocks[row][column].setPadding(blockPadding, blockPadding, blockPadding, blockPadding);

            tableRow.addView(blocks[row][column]);
            tableRow.setPadding(blockPadding, blockPadding, blockPadding, blockPadding);
        }
        tblCows.addView(tableRow,new TableLayout.LayoutParams(  
                LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));  
    }
}

请告诉我....谢谢。

【问题讨论】:

    标签: android android-layout tablelayout


    【解决方案1】:

    您已设置 TableRow 边缘的填充,而不是其元素之间的填充。

    您可以通过以下方式设置 TableRow 中每个单独视图的填充:

    for(int i = 0; i < tableRow.getVirtualChildCount(); i++){
        tableRow.getVirtualChildAt(i).setPadding(blockPadding, blockPadding, blockPadding, blockPadding);
    }
    

    这确实很愚蠢,但这是我现在能想到的最好的。

    【讨论】:

    • 我尝试了上面的代码,但在这里不起作用。我不明白原因....:(无论如何谢谢你的回复..fiction
    【解决方案2】:

    我找到了答案,通过对应用于按钮 Set margins in a LinearLayout programmatically 的布局参数使用 setmargin

    LayoutParams layoutParams = new LayoutParams(
        LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
    
    layoutParams.setMargins(30, 20, 30, 0);
    

    【讨论】:

      【解决方案3】:

      其他解决方案可能有效,但到目前为止我发现的最简单和最好的解决方案是在您需要间距的地方添加更多“空白”列。如果您正在动态地执行按钮,只需在按钮之间动态添加一个文本视图。不过,我只是在您尝试使用 TableLayout 时才建议这样做。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-16
        • 1970-01-01
        • 2020-11-12
        • 1970-01-01
        • 2015-01-11
        • 1970-01-01
        相关资源
        最近更新 更多