3

我正在尝试将 TableRows 添加到运行良好的 TableLayout 中。但是,我在布局参数方面遇到了一些问题。TableRow 中 TextView 之间的间距不像我想象的那样正常。

我当前的代码如下所示。

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 和它包含的 TextViews 继承现有 TableRow 和 TextView(s) 的布局属性。

4

2 回答 2

1

yawus 这是一个很好的教程,将帮助您设置表格布局 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);
于 2012-03-03T04:42:39.460 回答
1

您需要为每个表添加一个 RelativeLayout

TableRow row = new TableRow(this);
                            row.setId(tag);
                         // Creating a new RelativeLayout

                            RelativeLayout relativeLayout = new RelativeLayout(this);
                            // as the parent of that RelativeLayout is a TableRow you must use TableRow.LayoutParams
                            TableRow.LayoutParams rlp = new TableRow.LayoutParams(
                                    TableRow.LayoutParams.MATCH_PARENT,
                                    TableRow.LayoutParams.MATCH_PARENT);   
                            rlp.setMargins(0, 0, 0, 0);
                            row.addView(relativeLayout, rlp);

然后将 TextView 添加到 RelativeLayout。像这样

RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                          RelativeLayout.LayoutParams.WRAP_CONTENT,
                          RelativeLayout.LayoutParams.WRAP_CONTENT);
TextView payAmount = new TextView(this);
//lp.setMargins(left, top, right, bottom)
lp.setMargins(0, 0, 16, 0);
lp.addRule(RelativeLayout.ALIGN_LEFT);
payAmount.setLayoutParams(lp);
relativeLayout.addView(payAmount);

RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                          RelativeLayout.LayoutParams.WRAP_CONTENT,
                          RelativeLayout.LayoutParams.WRAP_CONTENT);
TextView payDate = new TextView(this);
//lp.setMargins(left, top, right, bottom)
lp.setMargins(0, 0, 16, 0);
lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
payDate.setLayoutParams(lp);
relativeLayout.addView(payDate);

这是我在表格行中处理定位界面组件的方式。

于 2013-05-27T11:29:37.853 回答