安卓:动态TableLayout [英] Android : Dynamic TableLayout

查看:180
本文介绍了安卓:动态TableLayout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是从数据库获取数据,然后显示它在我的应用程序表。

My goal is to get data from DB, and then display it as a table in my app.

下面是我的code:

 public void updateLastExpenses(){
    Cursor c = Expense.getAll(this);

    TableLayout lastExpensesTable = (TableLayout)findViewById(R.id.lastExpensesTable);

    lastExpensesTable.setStretchAllColumns(true);

    while (c.moveToNext()) {

        String name = c.getString(
                c.getColumnIndexOrThrow(ExpenseContract.ExpenseEntry.COLUMN_NAME_NAME)
        );
        float amount = c.getFloat(
                c.getColumnIndexOrThrow(ExpenseContract.ExpenseEntry.COLUMN_NAME_AMOUNT)
        );

        String date =  c.getString(
                c.getColumnIndexOrThrow(ExpenseContract.ExpenseEntry.COLUMN_NAME_DATE)
        );

        TableRow tr = new TableRow(this);

        TextView c1 = new TextView(this);
        c1.setText(name);
        c1.setBackgroundColor(Color.RED);

        TextView c2 = new TextView(this);
        c2.setText(""+amount);
        c2.setBackgroundColor(Color.BLUE);


        tr.addView(c1);
        tr.addView(c2);

        lastExpensesTable.addView(tr);

    }

}

这是我的TableLayout:

And here is my TableLayout:

<TableLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/lastExpensesTable">
            <TableRow
                android:id="@+id/lastExpensesTableRow"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <TextView
                    android:id="@+id/lastExpensesTableName"
                    android:text="@string/last_expenses_table_name"/>
                <TextView />
                <TextView
                    android:id="@+id/lastExpensesTableAmount"
                    android:text="@string/last_expenses_table_amount"/>
                <TextView />
            </TableRow>
        </TableLayout>

但这里是结果:

你知道为什么内容都是在名称列和两列之间的不拆?
是否有一个更清洁的方式做到这一点(通过使用布局,避免在java文件我猜的造型?)

Do you know why the content is all in the "name" column and not split between the two columns? Is there a cleaner way to do it (by using layout and avoiding the styling in the .java file I guess?)

先谢谢了。

推荐答案

您已经在布局流浪的TextView

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/lastExpensesTable">
        <TableRow
            android:id="@+id/lastExpensesTableRow"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <!-- first column -->
            <TextView
                android:id="@+id/lastExpensesTableName"
                android:text="@string/last_expenses_table_name"/>
            <!-- second column -->
            <TextView />
            <!-- third column -->
            <TextView
                android:id="@+id/lastExpensesTableAmount"
                android:text="@string/last_expenses_table_amount"/>
        </TableRow>
    </TableLayout>

取出了的TextView 标记为第二列和你所有的设置。

Take out that TextView marked "second column" and you're all set.

这篇关于安卓:动态TableLayout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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