我正在尝试将tablerows添加到tablelayout中,它工作得非常出色。但是,布局参数遇到了一些问题。tablerow中的textviews之间的间隔并不像我想象的那样工作。
我当前的代码如下。
paymentTable = (TableLayout) findViewById(R.id.paymentTable);
for(i = 0; i < cursor.getCount(); i++) {
TableRow row = new TableRow(this);
TextView payAmount = new TextView(this);
payAmount.setText(cursor.getString(cursor.getColumnIndex(DatabaseHelper.KEY_AMOUNT)));
payAmount.setPadding(payAmount.getPaddingLeft(),
payAmount.getPaddingTop(),
textView.getPaddingRight(),
payAmount.getPaddingBottom());
TextView payDate = new TextView(this);
payDate.setText(cursor.getString(cursor.getColumnIndex(DatabaseHelper.KEY_DATE)));
row.addView(payDate);
row.addView(payAmount);
paymentTable.addView(row, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
cursor.moveToNext();
}
之后,tablelayout的内容如下:
January$500
February$500
March$405
但我希望它看起来像:
January $500
February $500
March $405
为了澄清这一点,我希望每个新的tablerow及其包含的textview继承现有tablerow和textview的布局属性。
最佳答案
这是一个很好的教程,可以帮助你设置表格布局
http://www.anotherandroidblog.com/2010/08/04/android-database-tutorial/
这是XML布局
http://www.anotherandroidblog.com/2010/08/04/android-database-tutorial/6/
这是活动代码
http://www.anotherandroidblog.com/2010/08/04/android-database-tutorial/8/
TableRow tableRow= new TableRow(this);
ArrayList<Object> row = data.get(position);
TextView idText = new TextView(this);
idText.setText(row.get(0).toString());
tableRow.addView(idText);
TextView textOne = new TextView(this);
textOne.setText(row.get(1).toString());
tableRow.addView(textOne);
TextView textTwo = new TextView(this);
textTwo.setText(row.get(2).toString());
tableRow.addView(textTwo);
dataTable.addView(tableRow);
关于android - Android-使用现有TableRow布局将TableRow动态添加到TableLayout,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9542474/