2013-04-10 120 views
1

4個2x2的按鍵我想創建一個組2by2按鈕這樣相互對齊:如何使具有相同尺寸的

AB

CD

每一個都具有相同的寬度和無論其內容的長度如何。

我嘗試使用下面的設置與嵌套LinearLayouts,它符合我的要求,但是我收到一個警告說,嵌套layout_weight對性能有很大的(指數)的影響。

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:baselineAligned="false"> 

    <LinearLayout android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="50" 
     android:orientation="vertical"> 

     <Button 
      android:id="@+id/buttonA" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="50" 
      android:text="aaa"/> 

     <Button 
      android:id="@+id/buttonC" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="50" 
      android:text="cccccccccccccccccccccccccccc"/> 
    </LinearLayout> 

    <LinearLayout android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="50" 
     android:orientation="vertical"> 

     <Button 
      android:id="@+id/buttonB" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="50" 
      android:text="bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" /> 

     <Button 
      android:id="@+id/buttonD" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="50" 
      android:text="ddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"/> 
    </LinearLayout> 
    </LinearLayout> 

我不知道是否有更好的方法來做到這一點? 謝謝!

+0

更新我的XML - 似乎工作確定:-) – Darwind 2013-04-10 18:48:30

+0

感謝您的:)無法想象我們可以把一個tablerow的一個TableLayout之外。 – Ronald 2013-04-10 19:29:36

回答

1

使用LinearLayout和2 TableRows並在每個TableRow中插入2 Buttons。 分配widthheight因此您的需求。 指定的1 weight每個項目包括TableRows,你應該是好去。

編輯:好打了一下四周後,我發現這是工作好;-)

這基本上是自己的佈局和我;-)

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

    <TableRow 
     android:id="@+id/tableRow1" 
     android:layout_width="match_parent" 
     android:layout_height="0dip" 
     android:layout_weight="1" > 

     <Button 
      android:id="@+id/button1" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:text="cccccccccccccccccccccccccccc" /> 

     <Button 
      android:id="@+id/button2" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:text="Button" /> 
    </TableRow> 

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

     <Button 
      android:id="@+id/button3" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:text="bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" /> 

     <Button 
      android:id="@+id/button4" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:text="ddddddddddddddddddddddddddddddddddddddddddddddddddddddddd" /> 
    </TableRow> 

</LinearLayout> 
+0

這是我最初做的,但一些測試後,我發現這兩個行的高度可能是不同的,如果按鍵的第一行上的內容是高/短,那些在第二排。 – Ronald 2013-04-10 18:03:54

+0

是的,我看到的 - 我會做一些更多的測試,希望儘快給您;-) – Darwind 2013-04-10 18:28:07

0

我的避風港的混合沒有經過測試,但你可以使用button.getWidth()來獲得4個按鈕的寬度。找到4的最大值,然後將所有按鈕設置爲該寬度。如果按鈕的內容將是動態的。

+0

我會盡力的,但不必配置中的代碼佈局的最後一件事我會想這樣做。 – Ronald 2013-04-10 18:08:27

+0

由於設備種類繁多,這可能是您的最佳選擇。 – 2013-04-10 18:23:52

相關問題