2012-09-29 73 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"