2012-09-29 137 views
0

我試圖在Android中製作這樣的表格佈局。在Android中設置表格佈局

第一列 - 2個按鈕,下一列第二行一個按鈕。按鈕的大小應該相同。我使用0寬度和重量1並拉伸列。

Layout

在XML:

<TableLayout 
     android:id="@+id/tableLayout1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" 
     android:stretchColumns="*" > 

     <TableRow> 

      <Button 
       android:id="@+id/button_stereo_system_state" 
       style="@style/StyleButton" 
       android:background="@drawable/background_button_off" 
       android:drawableTop="@drawable/icon_button_on_off" 
       android:text="@string/button_text_option_off" /> 

      <Button 
       android:id="@+id/button_stereo_system_radio" 
       style="@style/StyleButton" 
       android:background="@drawable/background_button_off" 
       android:drawableTop="@drawable/icon_button_play" 
       android:text="@string/button_text_play_radio" /> 
     </TableRow> 
     <TableRow> 
      <Button 
       android:id="@+id/button_stereo_system_cd" 
       style="@style/StyleButton" 
       android:layout_column="0" 
       android:background="@drawable/background_button_off" 
       android:drawableTop="@drawable/icon_button_play" 
       android:text="@string/button_text_play_cd" /> 
     </TableRow> 
    </TableLayout> 

在樣式:

<style name="StyleButton"> 
     <item name="android:layout_height">wrap_content</item> 
     <item name="android:layout_width">0dip</item> 
     <item name="android:layout_weight">1</item> 
     <item name="android:layout_margin">10dip</item> 
     <item name="android:textSize">19sp</item> 
     <item name="android:textColor">@color/color_text_button</item> 
</style> 

隨着第二行按鈕此源代碼被拉伸並佔用了整個行。如何做到這一點?

+0

也許增加無形的按鈕? – Pawel

回答

1

一個解決方案(如您在評論中所猜測的)是添加與其餘部分相同的另一個按鈕,並將可見性設置爲不可見。 (請參閱文檔here)。

基本上只是將下面的行添加到您的XML:

android:visibility="invisible"