在相同的布局创建多个TableLayout [英] Creating more than one TableLayout in the same layout

查看:310
本文介绍了在相同的布局创建多个TableLayout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要与排在一个XML布局丰盛一些创建多个TableLayout。

I have a need to create more than one TableLayout with a varied number of rows in one XML layout.

在我的主要的布局我有一个空的LinearLayout,如下图所示:

In my main layout I have an empty LinearLayout, as shown below:

<LinearLayout android:id="@+id/resultsLayout"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:orientation="vertical">

</LinearLayout>

我在活动声明这是这样:

I declare this in my activity as so:

private LinearLayout linearResultsLayout;
linearResultsLayout = (LinearLayout) findViewById(R.id.resultsLayout);

我添加了一个table_layout.xml和table_row.xml到我的布局文件夹:

I have added a table_layout.xml and a table_row.xml to my Layouts folder:

Table_layout.xml:

Table_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/resultsTable"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:weightSum="99"
    android:background="@drawable/curvedbg"
    >


</TableLayout>

Table_row.xml:

Table_row.xml:

<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

     <TextView android:id="@+id/row_header" android:layout_weight="33" />
    <TextView android:id="@+id/downstream" android:layout_weight="33"/>
    <TextView android:id="@+id/upstream" android:layout_weight="33"/>

</TableRow>

我的目标是吹大表,然后抬高我怎么过多少行需要到新表膨胀,同时也传递数据TextViews的形式。下面的code应该给你什么,我试图做一个想法。但是这不工作。 NullPointerException异常。

My aim is to inflate the table and then inflate how ever many rows I require in to the newly inflated table whilst also passing data in the form of TextViews. The code below should give you an idea of what I'm trying to do. It doesn't work, however. NullPointerException.

private void createTable() {

        String[] downResults = {"Downstream","2000","98"};
        String[] upResults = {"Upstream","1000","78"};
        String[] rowHeadings = {"","Bandwidth","QoS"};

        TextView heading =  (TextView) findViewById(R.id.row_header);
        TextView downstream =  (TextView) findViewById(R.id.downstream);
        TextView upstream =  (TextView) findViewById(R.id.upstream);
        TableLayout newTable = (TableLayout) findViewById(R.id.resultsTable);

        View table = inflater.inflate(R.layout.table_layout, null);
        View row =  inflater.inflate(R.layout.table_row, null);
        linearResultsLayout.addView(table);

        for(int i = 0; i < rowHeadings.length; i++){
            heading.setText(rowHeadings[i].toString());
            downstream.setText(downResults[i].toString());
            upstream.setText(upResults[i].toString());
            newTable.addView(row);
        }

    }

我相信它会更加复杂,那么我如何,我想完成它,所以任何详细的指针将大大AP preciated。

I assume it will be more complicated then I'm how I'm trying to accomplish it, so any detailed pointers would be greatly appreciated.

推荐答案

您应该使用一个ListView:

You should probably use a ListView:

但是,假设你不能/不会,你有问题,你是引用了错误的布局时,你会得到你的的TextView 的对象。您code或许应该看起来更像是这样的:

But, assuming you can't / won't, the problem you have is that you are referencing the wrong layout when you get your TextView objects. Your code should probably look more like this:

    String[] downResults = {"Downstream","2000","98"};
    String[] upResults = {"Upstream","1000","78"};
    String[] rowHeadings = {"","Bandwidth","QoS"};

    View table = inflater.inflate(R.layout.table_layout, linearResultsLayout, false));
    View row =  inflater.inflate(R.layout.table_row, linearResultsLayout, false));

    TextView heading =  (TextView) row.findViewById(R.id.row_header);
    TextView downstream =  (TextView) row.findViewById(R.id.downstream);
    TextView upstream =  (TextView) row.findViewById(R.id.upstream);
    TableLayout newTable = (TableLayout) table.findViewById(R.id.resultsTable);

    linearResultsLayout.addView(table);

    for(int i = 0; i < rowHeadings.length; i++){
        heading.setText(rowHeadings[i].toString());
        downstream.setText(downResults[i].toString());
        upstream.setText(upResults[i].toString());
        table.addView(row);
    }

注意 row.findViewById ,并在 table.addView

编辑:注意布局吹气的变化

note the change in the layout inflater

这篇关于在相同的布局创建多个TableLayout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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