3

我有TableLayout,我想將按鈕作爲單元格。所有按鈕應具有相同的寬度。所有的按鈕應該有相同的高度。 (因爲我想填滿屏幕,重量和高度會有所不同)。Android TableLayout - 等寬和等高的單元格 - 不起作用,爲什麼

我確定我正在做的一切正確,但不幸的是只有寬度是相同的。問題在於高度 - 前兩行高度相同,第三行更小(或者更確切地說,只要按鈕不包含任何文本,高度相同,但是當我放置文本時,高度不相等)。我無法相信它不起作用,所以我甚至製作了屏幕快照,並在圖形程序中檢查了距離。

我的表:

<TableLayout 
    android:orientation="vertical" 
android:layout_width="match_parent" 
android:layout_height="0dip" 
android:layout_weight="1" 
android:stretchColumns="*" 
android:shrinkColumns="*" 
android:weightSum="3" 
android:padding="@dimen/dialog_margin" 
> 

    <TableRow 
     android:layout_width="match_parent" 
     android:layout_height="0dip" 
     android:layout_weight="1" 
     android:stretchColumns="*" 
     android:gravity="center" 
     android:weightSum="2"> 

     <Button 
      android:id="@+id/button_0" 
      android:layout_width="0dip" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      /> 
     <Button 
      android:id="@+id/button_1" 
      android:layout_width="0dip" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      /> 

    </TableRow> 

    <TableRow 
     android:layout_width="match_parent" 
     android:layout_height="0dip" 
     android:layout_weight="1" 
     android:stretchColumns="*" 
     android:gravity="center" 
     android:weightSum="2"> 

     // buttons analogously as in previous row.... 
    </TableRow> 

    <TableRow 
     android:layout_width="match_parent" 
     android:layout_height="0dip" 
     android:layout_weight="1" 
     android:stretchColumns="*" 
     android:gravity="center" 
     android:weightSum="2"> 

     // buttons analogously as in previous row.... 
    </TableRow> 

我想也設置TableRow高度match_parent。它沒有幫助。

和btw如何shrinkColums工作,特別是與stretchColumns一起? (我的版本有和沒有shrinkColumns,看起來沒有什麼區別

回答

0

我覺得,添加文本時不可能不改變寬度,但我認爲你可以試試這個,當你添加文字寫這樣的:

setText("<html>your text<html>"); 

它可能會幫助,但也許在全尺寸的文本不會在那裏

+0

請告訴我,如果它會工作,我也很好奇。 –

+0

O.對不起。它應該是。斜槓很重要 –

+0

http://docs.oracle.com/javase/tutorial/uiswing/components/html.htmlhttp://docs.oracle.com/javase/tutorial/uiswing/components/html.html –

0

使用此爲tableLayout

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

和這個每行

<TableRow 
     android:layout_width="match_parent" 
     android:layout_height="0dip" 
     android:layout_weight="1" 
     android:gravity="center"> 
+1

將此設置爲轉換,將TableLayout的layout_height設置爲match_parent不會更改任何內容。即使日食也暗示將0dip放在這裏。但在這兩種情況下它都不起作用(最後一行高度較小)。當它涉及到TableRow屬性時,它不起作用。 – user2707175

+0

奇怪...我在Eclipse中從xml複製了它,並且我正在獲取您正在尋找的內容。 – Merlevede

+0

奇怪,但在Eclipse中,所有字段也都有相同的高度。但是當我用Android 4.0.4在設備上運行它時,它們不會。此外,當我創建視圖時動態地添加文本,在Eclipse中,我沒有任何按鈕中的文本。然後他們有相同的高度。此外,由於文本不是非常長(即使每個按鈕的文本長度略有不同也不相同),並且即使前兩行較小,文本也適合它們,這也是令人驚訝的。我真的不明白爲什麼它不起作用。 – user2707175

相關問題