以编程方式使用android:layout_weight填充TableLayout

5
我正在尝试使用表格布局填充单元格和行以填满整个屏幕,就像这样:

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>

我尝试在代码中创建相同的外观,但目前失败了。这是我的Activity的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/

我在这里找不到我缺少的东西。 我也尝试在设计器中准备一个表格行并在运行时填充并添加到表格中,但结果没有改变。
所以,不知何故,我想以编程方式获得与编辑器创建的相同布局。
请帮忙!

我还尝试将上面的布局分成3个布局xml(一个用于表格,一个用于行,一个用于ImageView),并在运行时进行了填充,以查看是否缺少某些属性设置。然而,问题仍然存在。 - volkan
1个回答

6

我花了一个半小时来试图弄清楚发生了什么。然后我找到了:)这是我的TableActivity的完整列表,请注意使用不同的LayoutParams来处理表格行和图片:

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.widget.*;
import android.widget.TableLayout;
import android.widget.TableRow;

public class TableActivity extends Activity {

    private static final String TAG="TableActivity";

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.table_activity);

        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);

            //Note that you must use TableLayout.LayoutParams, 
            //since the parent of this TableRow is a TableLayout
            TableLayout.LayoutParams params = new TableLayout.LayoutParams(
                    TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT, 1f);

            for (int cell = 0; cell < 3; cell++) {
                ImageView image = new ImageView(this);
                image.setImageResource(android.R.drawable.btn_plus);
                //Note that here you must use TableRow.LayoutParams
                //since TableRow is the parent of this ImageView
                TableRow.LayoutParams imageParams = new TableRow.LayoutParams();

                //And this is how you set the gravity:
                imageParams.gravity = Gravity.CENTER;
                image.setLayoutParams(imageParams);
                tr.addView(image);
            }
            tr.setLayoutParams(params);
            tl.addView(tr, params);
        }
        tl.setStretchAllColumns(true);
    }
}

table_activity.xml是一个空的表格布局:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:id="@+id/table"
          android:layout_width="match_parent"
          android:layout_height="match_parent">
</TableLayout>

简而言之,最简单的解决方案是使用在xml文件中定义的布局 :) 但是如果你必须使用JAVA,你必须小心使用什么类型的LayoutParams,你应该始终使用视图父项的LayoutParams。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接