我正在嘗試填充單元格和行的表格佈局,以填充整個屏幕。就像這樣:以編程方式使用android:layout_weight填充TableLayout
http://imageshack.us/photo/my-images/69/device20120201005942.png/
這種佈局在Eclipse圖形佈局窗口設計給我想要的東西(安卓layout_weight解決縱向拉伸):
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:stretchColumns="*" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center" android:layout_weight="1">
<ImageView
android:id="@+id/imageView1"
android:layout_gravity="center"
android:src="@drawable/add" />
<ImageView
android:id="@+id/imageView2"
android:layout_gravity="center"
android:src="@drawable/bound" />
<ImageView
android:id="@+id/imageView3"
android:layout_gravity="center"
android:src="@drawable/cod" />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1" >
<ImageView
android:id="@+id/imageView4"
android:layout_gravity="center"
android:src="@drawable/delete" />
<ImageView
android:id="@+id/imageView5"
android:layout_gravity="center"
android:src="@drawable/delete2" />
<ImageView
android:id="@+id/imageView6"
android:layout_gravity="center"
android:src="@drawable/floppy" />
</TableRow>
</TableLayout>
我想創建中相同的外觀代碼但迄今爲止失敗。這裏是我的活動的OnCreate:我缺少的代碼來設置ImageView的重力
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tablelo);
TableLayout tl = (TableLayout) findViewById(R.id.table);
Log.d(TAG, "Width :" + tl.getWidth());
for (int row = 0; row < 3; row++) {
TableRow tr = new TableRow(this);
tr.setId(100 + row);
for (int cell = 0; cell < 3; cell++) {
ImageView image = new ImageView(this);
image.setImageResource(imageIDs[row * 3 + cell]);
tr.addView(image);
}
tr.setGravity(Gravity.CENTER);
LayoutParams params = new TableRow.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 1);
tr.setLayoutParams(params);
tl.addView(tr, params);
}
tl.setStretchAllColumns(true);
}
,但這學嘗試將導致崩潰:
image.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER));
該代碼產生3行創建的,每個伸展水平,但不垂直:
http://imageshack.us/photo/my-images/835/gridc.png/
我無法找到我是缺少在這裏。我也嘗試在設計器中準備一個表格,並在運行時膨脹並添加到表格中,但結果沒有改變。
所以,不知何故,我想以編程方式獲得我可以用編輯器創建的相同佈局。
請幫忙!
我也試圖將上面的佈局分成3個佈局xmls(一個用於表格,一個用於行,一個用於imageview),並在運行時將它們充氣以查看我是否缺少一些要設置的屬性。但仍然是同樣的問題。 – volkan 2012-02-01 19:05:08