Android - 使用现有的 TableRow 布局将 TableRow 动态添加到 TableLayout [英] Android - Dynamically Adding TableRow to TableLayout using an existing TableRow layout

查看:74
本文介绍了Android - 使用现有的 TableRow 布局将 TableRow 动态添加到 TableLayout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 TableRows 添加到 TableLayout,它工作得非常好.但是,我在布局参数方面遇到了一些问题.TableRow 中 TextViews 之间的间距不像我想象的那样工作.

I'm trying to adding TableRows to a TableLayout, which is working fantastically. However, I'm running into some problems with layout parameters. The spacing between the TextViews within a TableRow isn't working out like I thought it would.

我当前的代码如下所示.

My current code looks as follows.

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 看起来像:

Afterwards, the TableLayout looks something along the lines of:

January$500

February$500

March$405

但我希望它看起来像:

January    $500
February   $500
March      $405

为了澄清事情,我希望每个新的 TableRow 和它包含的 TextViews 继承现有 TableRow 和 TextView(s) 的布局属性.

To clarify things, I want each new TableRow and the TextViews it contains to inherit layout properties of an existing TableRow and TextView(s).

推荐答案

yawus 这是一个很好的教程,将帮助您设置表格布局http://www.anotherandroidblog.com/2010/08/04/android-database-tutorial/

yawus this is a good tutorial will help you set the table layout 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 - 使用现有的 TableRow 布局将 TableRow 动态添加到 TableLayout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆