如何以编程方式在表格行中从右到左添加对象

时间:2016-05-17 14:03:09

标签: android android-layout android-tablelayout

我有一个表格布局和一个表格行。我可以通过以下代码在代码上添加按钮:

for (int row = 0; row < buttonTableLayout.getChildCount(); ++row)
((TableRow) buttonTableLayout.getChildAt(row)).removeAllViews();
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

for (int row = 0; row < 3; row++) {
TableRow currentTableRow = getTableRow(row);
                for (int column = 0; column < 7; column++) {
                    Button newGuessButton = (Button)inflater.inflate(R.layout.my_button, currentTableRow, false);
                    String myName = new String(String.valueOf((row * 7) + column + 1));
                    newGuessButton.setText(myName);
                    currentTableRow.addView(newGuessButton);
                }
            }
 private TableRow getTableRow(int row) {
    return (TableRow) buttonTableLayout.getChildAt(row);
}

但我想添加从RIGHT到LEFT的按钮。不只是从右边对齐,比如:

enter image description here

感谢。

1 个答案:

答案 0 :(得分:1)

您可以使用TableRow

更改currentTableRow.setGravity(Gravity.RIGHT)重力

然后反转你的for循环,以保持项目顺序:

for (int column = 6; column >= 0; column--) {
     Button newGuessButton = (Button)inflater.inflate(R.layout.my_button, currentTableRow, false);
     String myName = new String(String.valueOf((row * 7) + column + 1));
     newGuessButton.setText(myName);
     currentTableRow.addView(newGuessButton);
}