Android动态填充表格与布局

时间:2014-01-26 12:06:01

标签: android

我尝试使用预定义的行和字段布局填充Android上的表格。结果应该是一个6x5(或6x4)表,只有彩色字段,有黑色边框。

所以我创建了活动布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="abc" />
    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:stretchColumns="*"
        android:shrinkColumns="*"
        android:background="#000000"
        android:id="@+id/beertable">
    </TableLayout>

</LinearLayout>

这是Row布局:

<?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="wrap_content">

</TableRow>

这里的字段布局:

<?xml version="1.0" encoding="utf-8"?>
<View xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="30dip"
    android:background="#ffc35a"
    android:layout_margin="1dip">
</View>

在我的Activity中,我尝试使用for循环来插入行和字段,但是当我测试此代码时,我只看到文本“abc”,然后屏幕的其余部分是字段的颜色。没有边框,我无法看到桌子的起点和终点。

public class WG extends Activity {    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_wg);
        TableLayout table = (TableLayout) findViewById(R.id.beertable);

        TableRow ll;
        View beerField;

        for(int i = 0;i<6;i++)
        {
            ll = (TableRow) getLayoutInflater().inflate(R.layout.wg_beer_row, null);
            table.addView(ll);
            for(int a=0;a<5;a++)
            {
                beerField = (View) getLayoutInflater().inflate(R.layout.wg_beer_field, null);
                ll.addView(beerField);
            }   

        }    
    }
}

有人可能会告诉我这个错误吗?

1 个答案:

答案 0 :(得分:1)

  

有人可能会告诉我这个错误吗?

为您的虚增视图提供正确的LayoutParams

ll = (TableRow) getLayoutInflater().inflate(R.layout.wg_beer_row, table, false);

beerField = (View) getLayoutInflater().inflate(R.layout.wg_beer_field, ll, false);