Android的布局:TableView中

问题描述:

谁能告诉我如何做以下Android的布局:TableView中

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="fill_parent" 
     android:gravity="center" 
     android:layout_height="wrap_content" > 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="#300000" 
     android:textColor="#ff0000" 
     android:text="Sample Layout"/> 
    </LinearLayout> 
    <TableLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 
    <TableRow> 
    <Button 
     android:layout_column="1" 
     android:layout_height="wrap_content" 
     android:background="@drawable/icon" 
     android:padding="2dip"/> 
    <Button 
     android:layout_column="2" 
     android:layout_height="wrap_content" 
     android:background="@drawable/icon" 
     android:padding="2dip"/> 
    <Button 
     android:layout_column="3" 
     android:layout_height="wrap_content" 
     android:background="@drawable/icon" 
     android:padding="2dip"/> 
    </TableRow> 
    <TableRow> 
    <TextView 
     android:layout_column="1" 
     android:layout_height="wrap_content" 
     android:padding="2dip" 
     android:text="News Feed"/> 
    <TextView 
     android:layout_column="2" 
     android:layout_height="wrap_content" 
     android:text="Profile" 
     android:padding="2dip"/> 
    <TextView 
     android:layout_column="3" 
     android:layout_height="wrap_content" 
     android:text="Friends" 
     android:padding="2dip"/> 
    </TableRow> 
    <TableRow> 
    <Button 
     android:layout_column="1" 
     android:layout_height="wrap_content" 
     android:background="@drawable/icon" 
     android:padding="2dip"/> 
    <Button 
     android:layout_column="2" 
     android:layout_height="wrap_content" 
     android:background="@drawable/icon" 
     android:padding="2dip"/> 
    <Button 
     android:layout_column="3" 
     android:layout_height="wrap_content" 
     android:background="@drawable/icon" 
     android:padding="2dip"/> 
    </TableRow> 
    <TableRow> 
    <TextView 
     android:layout_column="1" 
     android:layout_height="wrap_content" 
     android:text="Messages" 
     android:padding="5dip"/> 
    <TextView 
     android:layout_column="2" 
     android:layout_height="wrap_content" 
     android:text="Places" 
     android:padding="5dip"/> 
    <TextView 
     android:layout_column="3" 
     android:layout_height="wrap_content" 
     android:text="Groups" 
     android:padding="5dip"/> 
    </TableRow> 
    </TableLayout> 
</LinearLayout> 

My layout

看起来更像低于目标比如我的样品。填充似乎没有按预期工作,我也认为需要将整个表格布局居中。有人可以帮助我了解发生了什么问题吗?而我需要修改以匹配目标布局(3x2而不是3x3)?谢谢。

Target Layout

使用ImageButton代替Button,并使用嵌套LinearLayout在每个表格单元格中正确中心的每个按钮下面的文本:

<TableRow> 
    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" > 
     <ImageButton 
      android:layout_gravity="center_horizontal" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="#00000000" 
      android:src="@drawable/icon" /> 
     <TextView 
      android:layout_gravity="center_horizontal" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:padding="2dip" 
      android:text="News Feed"/> 
    </LinearLayout> 

    <LinearLayout> 
     ... 
    </LinearLayout> 
</TableRow> 

通知android:layout_gravity="center_horizontal"该水平为中心的视图父视图。此代码可能需要一些改进,但这是您应该遵循的一般想法(使用layout_gravity,嵌套布局和ImageButtons)。

您还可以改善你的表格布局,它将使用可用的整个空间:

<TableLayout 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:stretchColumns="*" > 
+0

太好了!使用android:stretchColumns,表格现在居中。谢谢!这肯定会改善一些事情。只是好奇,我使用ImageButtons,因为按钮没有文字?另外,我注意到你在文本中包含了填充,而不是实际的ImageButton(但ImageButton看起来有填充)。你如何去除ImageButton周围的填充? – 2011-12-17 18:11:22

+0

当您想要显示图像而不是按钮时,ImageButtons会更好。在这里,我将按钮背景设置为透明(颜色#00000000),但它可以具有背景以及图像。我会建议尝试找到类似应用程序的源代码以了解如何继续。你会节省很多时间,因为android布局有时候会非常棘手...... – Dalmas 2011-12-17 18:19:00

+0

我没有看到我在TextView周围设置了填充,但是如果你想对ImageButton做同样的操作,请使用边距而不是填充( android:marginLeft,marginRight等)。 – Dalmas 2011-12-17 18:21:03