如何动态设置表格布局中列的填充
我正在尝试动态创建带有按钮的表格布局。我可以通过按钮获取表格布局。 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我找到了答案,通过对应用于按钮的布局参数使用 setmargin 以编程方式在 LinearLayout 中设置边距
i found answer, by using setmargin to the layout params applied to button Set margins in a LinearLayout programmatically
您已经设置了 TableRow 边缘的填充,而不是其元素之间的填充。
您可以通过这样做来设置 TableRow 中每个单独视图的填充:
这确实很愚蠢,但这是我现在能想到的最好的。
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:
This is really stupid, but it is the best I can think of right now.
其他解决方案可能有效,但迄今为止我发现的最简单和最好的解决方案是在需要间距的地方添加更多“空白”列。如果您动态执行按钮,只需在按钮之间动态添加文本视图即可。如果您尝试使用 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.