2014-02-18 73 views
0

我在設置TableLayout中列的寬度時遇到問題。這個問題已經在前面討論過:
How to set Table width and number of Columns in TableLayout in Android
How to get a TableLayout with 2 columns of equal width
Set equal width of columns in table layout in Android
,但我沒有找到一個解決方案。下面的代碼的確將列設置爲相等的寬度,但似乎將第一列中的按鈕關閉。由於 第二列中的按鈕文本較寬,因此它以不利的方式顯示。設置TableLayout中的列寬度

TableLayout

所需的顯示:

enter image description here

<TableRow 
    android:id="@+id/tableRow1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" > 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="horizontal" > 

     <Button 
      android:id="@+id/menu1Button" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_margin="4dp" 
      android:layout_weight="1" 
      android:background="@color/darkgrey2" 
      android:contentDescription="@string/menu1" 
      android:onClick="click1" 
      android:padding="60dp" 
      android:text="hello" 
      android:textColor="@color/white" 
      android:textSize="32sp" 
      android:textStyle="bold" /> 

     <Button 
      android:id="@+id/menu2Button" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_margin="4dp" 
      android:layout_weight="1" 
      android:background="@color/darkgrey2" 
      android:contentDescription="@string/menu1" 
      android:onClick="click1" 
      android:padding="60dp" 
      android:text="hellothere" 
      android:textColor="@color/white" 
      android:textSize="32sp" 
      android:textStyle="bold" /> 
    </LinearLayout> 
</TableRow> 

<TableRow 
    android:id="@+id/tableRow2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" > 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="horizontal" > 

     <Button 
      android:id="@+id/menu3Button" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_margin="4dp" 
      android:layout_weight="1" 
      android:background="@color/darkgrey2" 
      android:contentDescription="@string/menu1" 
      android:onClick="click1" 
      android:padding="60dp" 
      android:text="hello" 
      android:textColor="@color/white" 
      android:textSize="32sp" 
      android:textStyle="bold" /> 

     <Button 
      android:id="@+id/menu4Button" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_margin="4dp" 
      android:layout_weight="1" 
      android:background="@color/darkgrey2" 
      android:contentDescription="@string/menu1" 
      android:onClick="click1" 
      android:padding="60dp" 
      android:text="hello" 
      android:textColor="@color/white" 
      android:textSize="32sp" 
      android:textStyle="bold" /> 
    </LinearLayout> 
</TableRow> 

編輯: 卸下邊距和填充結果: enter image description here

EDIT2: 只去掉保證金: enter image description here

+0

settin會有什麼影響g'android:maxLines =「1」'在'Button'的佈局中? – nKn

+0

我在「hellothere」按鈕中添加了建議的行,它確實將其限制爲一行,但結果是該按鈕讀作「helloth」。這種情況下的所有按鈕都是相同的寬度和高度,只適合「你好」 –

+0

這可能是由按鈕的「padding」屬性產生的。由於四邊(左邊,右邊,頂部,底部)的位置有所不同,它會減少可以顯示文本的佈局,可能這是問題,文本會被包裹,因爲沒有足夠的空間來顯示邊距線。 '60dp'確實是一個大的空間減少,試着大幅度減少它,看看會發生什麼。 – nKn

回答

1

我想你將無法實現這種方式,因爲你的寬度基於weight代替width財產。既然你使所有按鈕的寬度都取決於最大寬度的寬度,爲了得到這個效果,我會使用不同的方法,基於選擇最大寬度。所有按鈕的寬度,並將其設置爲其餘部分。如果你有一個靜態數量的按鈕,或者你可以跟蹤你的佈局中的每個按鈕,這將工作。因此,恢復:

1)從佈局中刪除layout_weight屬性。

2)您可以在layout_width財產留給0,如果你不知道分配給他們在創建視圖時文本,或將其設置爲wrap_content否則。

3)在您的onCreate()或那種方法,做這樣的事情(這是一個僞代碼):

int maxwidth = -1; 

// Firstly, get the maximum width 
for (Button myButton : my_buttons) 
    if (myButton.getWidth() > maxwidth) 
    maxwidth = myButton.getWidth(); 

// Assign it to all your Buttons 
for (Button myButton : my_buttons) 
    myButton.setWidth(maxwidth); 
+1

這是有效的。這些按鈕是靜態的,只需要步驟1和步驟2,但是如果需要的話,它很好地知道如何做3。 –

1

設置的TableRow的寬度match_parent,它會exapand與它的孩子elemnts即你的按鈕

<TableRow 
    android:id="@+id/tableRow2" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 

不知道,如果想的LinearLayout,

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:orientation="vertical" > 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:padding="20dp" 
     android:text="Hello" /> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:padding="20dp" 
     android:text="Hello There" /> 
</LinearLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:padding="20dp" 
     android:text="Button" /> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:padding="20dp" 
     android:text="Button" /> 
</LinearLayout> 

+0

感謝您的回覆,但這沒有什麼區別。 –

+0

您正在使用android的邊距:layout_margin =「4dp」,請嘗試刪除此項。並嘗試填充。 –

+0

我刪除了您的建議的邊距和填充。請參閱上文原始文章 –

0

你必須在你的按鈕聲明中添加以下內容:

android:singleLine="true" 

希望這有助於:)

+0

我將你建議的行添加到「hellothere」按鈕,它確實將其限制爲一行,但導致該按鈕讀作「hellot ...」。這種情況下的所有按鈕都是相同的寬度和高度,只適用於「你好」 –

1

試試這個代碼:

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="1" 
    android:orientation="horizontal" > 

    <TableRow 
     android:id="@+id/tableRow1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" > 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:orientation="horizontal" > 

      <Button 
       android:id="@+id/menu1Button" 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_margin="4dp" 
       android:layout_weight="1" 
       android:contentDescription="Button" 
       android:onClick="click1" 
       android:padding="60dp" 
       android:text="hello" 
       android:textSize="32sp" 
       android:textStyle="bold" /> 

      <Button 
       android:id="@+id/menu2Button" 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_margin="4dp" 
       android:layout_weight="1" 
       android:contentDescription="Button2" 
       android:onClick="click1" 
       android:padding="60dp" 
       android:text="hellothere" 
       android:textSize="32sp" 
       android:textStyle="bold" /> 
     </LinearLayout> 
    </TableRow> 
</LinearLayout> 

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="1" 
    android:orientation="horizontal" > 

    <TableRow 
     android:id="@+id/tableRow2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" > 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:orientation="horizontal" > 

      <Button 
       android:id="@+id/menu3Button" 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_margin="4dp" 
       android:layout_weight="1" 
       android:onClick="click1" 
       android:padding="60dp" 
       android:text="hello" 
       android:textSize="32sp" 
       android:textStyle="bold" /> 

      <Button 
       android:id="@+id/menu4Button" 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_margin="4dp" 
       android:layout_weight="1" 
       android:onClick="click1" 
       android:padding="60dp" 
       android:text="hello" 
       android:textSize="32sp" 
       android:textStyle="bold" /> 
     </LinearLayout> 
    </TableRow> 
</LinearLayout>