我需要在一个XML布局中创建多个具有不同行数的TableLayout。
在我的主布局中,我有一个空的LinearLayout,如下所示:
<LinearLayout android:id="@+id/resultsLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
我在我的活动中声明了这一点:
private LinearLayout linearResultsLayout;
linearResultsLayout = (LinearLayout) findViewById(R.id.resultsLayout);
我已将table_layout.xml和table_row.xml添加到我的Layouts文件夹中:
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:
<?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的形式传递数据。下面的代码应该让您了解我尝试做什么。然而,它并不起作用。的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);
}
}
我认为它会比我试图完成它更复杂。
答案 0 :(得分:0)
您应该使用ListView:
但是,假设你不能/不会,你遇到的问题是当你得到TextView
个对象时你引用了错误的布局。您的代码应该看起来更像这样:
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
编辑:注意布局inflater的变化