在讀取StackOverflow上的數十個線程後。 我想動態添加行到表格佈局。 問題是,只有一行是動態添加的,並且它跨越所有頁面。 這是我的代碼:android tablelaayout動態添加行後添加最後一行
package com.example.trashproject;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TableRow.LayoutParams;
public class MainActivity extends FragmentActivity{
private static final int ROWS =2;
private static final int COLS = 10;
private static final String TAG = null;
private TableLayout mTable;
private View[][] mCircles;
private boolean[][] mData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
makeTable();
}
private void makeTable() {
mTable = (TableLayout) findViewById(R.id.table);
TableRow.LayoutParams rowParams = new TableRow.LayoutParams();
rowParams.width = LayoutParams.WRAP_CONTENT;
rowParams.height = LayoutParams.WRAP_CONTENT;
mCircles = new View[ROWS][COLS];
TableRow row;
for (int i = 0; i < ROWS; i++) {
row = new TableRow(this);
row.setLayoutParams(rowParams);
for (int j = 0; j < COLS; j++) {
mCircles[i][j] = new View(this);
mCircles[i][j].setBackgroundResource(R.drawable.small_circle);
row.addView(mCircles[i][j]);
}
mTable.addView(row);
}
}
activity_main.xml中:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TableLayout
android:id="@+id/table"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TableRow >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEXT"/>
</TableRow>
<TableRow >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEXT"/>
</TableRow>
</TableLayout>
</LinearLayout>
calendar_month_grid.xml:
small_circle.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<size android:width="15dp"
android:height="1dp"/>
<solid android:color="@android:color/holo_green_light"/>
</shape>
您的幫助表示讚賞。 ..
不知道這是否會有所幫助,但我會擺脫表的xml文件中的錶行定義。 –
@Kristy welsh:我已經添加它只是爲了檢查它是否只有朝氣添加的行不工作... –