如何动态设置表格布局中列的填充

发布于 2024-10-09 10:04:53 字数 1015 浏览 0 评论 0原文

我正在尝试动态创建带有按钮的表格布局。我可以通过按钮获取表格布局。 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));  
    }
}

请告诉我......谢谢。

I am trying to create a table layout with buttons dynamically. I am able to get the table layout with buttons. bt i need padding between buttons. How i can get programatically.

I tried following code 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));  
    }
}

Please let me know.... Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

海夕 2024-10-16 10:04:53

我找到了答案,通过对应用于按钮的布局参数使用 setmargin 以编程方式在 LinearLayout 中设置边距

LayoutParams layoutParams = new LayoutParams(
    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);

layoutParams.setMargins(30, 20, 30, 0);

i found answer, by using setmargin to the layout params applied to button Set margins in a LinearLayout programmatically

LayoutParams layoutParams = new LayoutParams(
    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);

layoutParams.setMargins(30, 20, 30, 0);
美男兮 2024-10-16 10:04:53

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

您可以通过这样做来设置 TableRow 中每个单独视图的填充:

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

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

You've been set padding of the edges of the TableRow, not between its elements.

You can set padding of every individual view in the TableRow by doing this:

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

This is really stupid, but it is the best I can think of right now.

蓝眸 2024-10-16 10:04:53

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

Other solutions may work, but the easiest and best solution I have found so far is to add more "blank" columns where you need the spacing. If you are doing the buttons dynamically just add a text view between the buttons dynamically. I am only suggesting this if you are trying to use a TableLayout though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文