2011-10-13 96 views
2

這是問題所在。Android TableLayout延伸單元格內容

我創建了一個包含一些ImageButton的TableLayout。

每行有3個ImageButton,但它們的寬度不等於表的寬度。 同樣適用於他們的身高。

所有我想要做的是中心,他們都在細胞內的ImageButtons。

auto:StretchColumns="*" 

拉伸ImageButtons(小區數據),即使我已經在XML文件中指定的大小。

此外,有沒有任何選項可以做同樣的行?

類似於自動計算單元格的邊距。

回答

2

您是否嘗試過爲ImageButton設置android:layout_gravity="center"

或者,你可以在一個LinearLayout像這樣的包裝每個ImageButton

<LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent"> 
    <ImageButton android:layout_gravity="center" ...other attributes... /> 
</LinearLayout> 
+0

是的,我確實嘗試過不起作用。我正在嘗試一種不同的方法,而你說它可以工作的方式將使應用程序不必要地更重(不是很多,但它會) – ikromm

+0

你可以嘗試發佈佈局文件或其樣本嗎? – kaspermoerch

+0

沒有必要。即使它有點重,它仍能正常工作。歡呼:) – ikromm

1

表佈局的兒童不必是唯一的TableRow。表格佈局支持所有類型的子類,包括LinearLayout,TextView,ImageButton等。

不要將圖像按鈕封裝在TableRow中。將它作爲一行插入。

<TableRow> 
    <TextView 
     android:id="@+id/tv1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Example Table Row" /> 
</TableRow> 

<ImageButton 
    android:id="@+id/ib1" 
    android:layout_width="fill_parent" <---! The Parent Is The Table Layout !---> 
    android:layout_height="wrap_content" 
    android:background="@drawable/coolBGImage" /> 

<TableRow> 
BLAH BLAH BLAH 
</TableRow> 

爲了把3個ImageButtons一行使用的LinearLayout與android:orientation="horizontal"

</TableRow> 

<LinearLayout 
    android:id="@+id/LL1" 
    android:layout_width="fill_parent" <---! The Parent is the TableLayout !---> 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 

     <ImageButton 
      android:id="@+id/IB1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@drawable/coolBGImage1" 
      android:weight="1" /> 

添加接下來的兩個圖像按鈕具有相同的權重,以使按鈕平均分配行。

</LinearLayout>