2010-05-19 47 views
36

下面是從我的XML的一部分土地格式:XML表格佈局?兩個等寬的行填充了等寬的按鈕?

<TableLayout 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:layout_gravity="center" 
    android:stretchColumns="*"> 
<TableRow>  
    <Button 
     android:id="@+id/countbutton" 
     android:text="@string/plus1"/>  
    <Button 
     android:id="@+id/resetbutton" 
     android:text="@string/reset" 
     /> 
</TableRow> 
</TableLayout> 

現在我不明白 - 一個排的寬度,也該按鈕取決於按鈕中的文本。如果這兩個文本是equal long可以說:TEXT它確定 - 半桌在屏幕中間。但如果他們有不同的尺寸 - 讓我們說「A」和「這是一個長按鈕」,桌子的中心不再位於屏幕中間,所以按鈕的寬度不是同樣的...

回答

87

要行中的按鈕與您需要做的尺寸相同。

<LinearLayout android:orientation="horizontal" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 
     <Button android:layout_weight="1" 
      android:layout_height="wrap_content" 
      android:layout_width="0dip"/> 
     <Button android:layout_weight="1" 
      android:layout_height="wrap_content" 
      android:layout_width="0dip"/> 
    </LinearLayout> 

並填寫您的按鈕的其他xml屬性。

魔術是在layout_weight和width屬性中。你不需要表格佈局。這些屬性告訴佈局,您的視圖應該在父佈局中佔用相等的空間。

+0

一個人如何在代碼中動態創建的按鈕設置這些值? – 2011-09-11 14:28:21

+0

parant.addView(newView,new LinearLayout.LayoutParams(0/* width * /,WRAP_CONTENT,1/* weight * /)); – 2011-09-12 03:32:26

+1

如果您是通過搜索找到的,請注意您需要在LinearLayout上執行android:layout_width =「fill_parent」或其他值。我在運行時遇到了有關如何指定layout_width的錯誤,並且花了我一段時間才發現它是在抱怨LinearLayout,而不是包含具有零值佈局寬度的項目。另請參閱http:// stackoverflow。COM /問題/ 1177020/Android系統如何對化妝的所有元素 - 內 - LinearLayout中,相同大小。 – Unoti 2011-11-30 00:49:37

2

除了接受的答案:

我其中我需要若干圖像中具有相等的列寬的柵格類似的問題,所以就用一個表的佈局。它工作,但作爲圖像異步加載,相應的列將佔用整個寬度,直到所有列都至少有一個圖像。

我使用Robby Pond的解決方案解決了這個問題,但它並不適用於最後一行,它不一定與其他行具有一樣多的圖像,伸展這些圖像以佔用我想要的所有可用空間以適應與上述相同的列。爲了解決這個問題,我填寫該行的剩餘空列與普通視圖對象,

使用相同的佈局參數,所有其他的圖像:

width = 0, weight = 1.這解決它!

0

佈局片斷

<TableLayout 
    android:id="@+id/tablelayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

以編程設定在表中的按鈕的佈局屬性的代碼:

public void addButtons(View view) { 
    TableLayout tableLayout = (TableLayout) findViewById(R.id.tablelayout); 
    Context context = getApplicationContext(); 
    tableLayout.removeAllViews(); 

    for (int rowIndex = 0; rowIndex < ROWS; rowIndex++) { 
     TableRow row = new TableRow(context); 
     for (int columnIndex = 0; columnIndex < COLUMNS; columnIndex++) { 
      Button btn = new Button(context); 
      LayoutParams buttonParams = new LayoutParams(0, 
        LayoutParams.WRAP_CONTENT, 1f); 
      btn.setLayoutParams(buttonParams); 
      row.addView(btn); 
     } 
     tableLayout.addView(row); 
    } 
} 
4

尼斯(從http://androidadvice.blogspot.com/2010/10/tablelayout-columns-equal-width.html原本)例如

測試和工作:

<TableRow> 
    <!-- Column 1 --> 
    <TextView 
    android:id="@+id/tbl_txt1" 
    android:layout_width="0dip" 
    android:layout_height="wrap_content" 
    android:background="@color/red" 
    android:textColor="@color/white" 
    android:padding="10dip" 
    android:layout_margin="4dip" 
    android:layout_weight="1" 
    android:text="Column 1" /> 

    <!-- Column 2 --> 
    <TextView 
    android:id="@+id/tbl_txt2" 
    android:layout_width="0dip" 
    android:layout_height="wrap_content" 
    android:background="@color/red" 
    android:textColor="@color/white" 
    android:padding="10dip" 
    android:layout_margin="4dip" 
    android:layout_weight="1" 
    android:text="Column 2" /> 

    <!-- Column 3 --> 
    <TextView 
    android:id="@+id/tbl_txt3" 
    android:layout_width="0dip" 
    android:layout_height="wrap_content" 
    android:background="@color/red" 
    android:textColor="@color/white" 
    android:padding="10dip" 
    android:layout_margin="4dip" 
    android:layout_weight="1" 
    android:text="Column 3" /> 
</TableRow>