テーブルレイアウトにセルと行ストレッチを入力して画面全体に表示しようとしています。このような:
http://imageshack.us/photo/my-images/69/device20120201005942.png/
Eclipseのグラフィカルレイアウトウィンドウで設計されたこのレイアウトは、私が望むものを提供します(android: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です:
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);
}
ImageViewの重力を設定するコードがありませんが、この試みによりクラッシュが発生します。
image.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER));
このコードにより、3つの行が作成され、それぞれが水平方向に伸びますが、垂直方向には伸びません。
http://imageshack.us/photo/my-images/835/gridc.png/
ここで欠けているものが見つかりません。また、デザイナーでテーブル行を準備し、実行時にテーブルを膨らませて追加しようとしましたが、結果は変わりませんでした。
それで、どういうわけか、私はエディターで作成できるのと同じレイアウトをプログラムで取得したいと思います。
助けてください!