![](/img/trans.png)
[英]Is it possible to add more than one TableLayout in a LinearLayout with a ScrollView in Android development?
[英]Creating more than one TableLayout in the same layout
我需要在一個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:
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的形式傳遞數據。 下面的代碼應使您對我要執行的操作有所了解。 但是,它不起作用。 空指針異常。
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);
}
}
我認為這將比我要完成它要復雜得多。
您可能應該使用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
編輯:注意布局充氣機中的更改
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.