2012-01-31 35 views
4

我正在嘗試填充單元格和行的表格佈局,以填充整個屏幕。就像這樣:以編程方式使用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/

我無法找到我是缺少在這裏。我也嘗試在設計器中準備一個表格,並在運行時膨脹並添加到表格中,但結果沒有改變。

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

請幫忙!

+0

我也試圖將上面的佈局分成3個佈局xmls(一個用於表格,一個用於行,一個用於imageview),並在運行時將它們充氣以查看我是否缺少一些要設置的屬性。但仍然是同樣的問題。 – volkan 2012-02-01 19:05:08

回答

5

我花了一個半小時試圖弄清楚發生了什麼事情。然後我發現它:)這是我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王,你應該總是使用一個視圖的父項。

相關問題