簡體   English   中英

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

[英]Populate TableLayout with android:layout_weight programmatically

我試圖填充一個tablelayout與單元格和行拉伸填充整個屏幕。 像這樣:

http://imageshack.us/photo/my-images/69/device20120201005942.png/

在Eclipse的Graphical Layout窗口中設計的這個布局給出了我想要的東西(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>

我試圖在代碼中創建相同的外觀但到目前為止失敗。 這是我的活動的創建:

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/

我找不到我在這里失蹤的東西。 我還嘗試在設計器中准備一個tablerow並在運行時添加並添加到表中,但結果沒有改變。

所以,不知何故,我希望以編程方式獲得我可以使用編輯器創建的相同布局。

請幫忙!

我花了一個半小時試圖弄清楚發生了什么。 然后我發現它:)這是我的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之王,你應該總是使用一個視圖的家長。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM