Android TableLayout可以扩展单元格内容

时间:2011-10-13 10:26:48

标签: android tablelayout imagebutton stretch

这是问题所在。

我正在创建一个包含一些ImageButtons的TableLayout。

每行有3个ImageButtons,但它们的宽度不等于表格的宽度。 同样适用于他们的身高。

我想要做的就是将ImageButtons置于其所在的单元格中。

auto:StretchColumns="*"

拉伸ImageButtons(单元格数据),甚至我已经在XML文件中指定了它们的大小。

此外,是否有任何选项可以为行做同样的事情?

类似于自动计算单元格的边距。

2 个答案:

答案 0 :(得分:2)

您是否尝试过设置ImageButtons的android:layout_gravity="center"

或者,您可以将每个ImageButton打包成LinearLayout,如下所示:

<LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent">
   <ImageButton android:layout_gravity="center" ...other attributes... />
</LinearLayout>

答案 1 :(得分:1)

表格布局的子项不必只是TableRow。表格布局支持所有类型的孩子,包括LinearLayout,TextView,ImageButton等。

不要将图像按钮包裹在TableRow中。将其插入一行。

<TableRow>
    <TextView
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Example Table Row" />
</TableRow>

<ImageButton
    android:id="@+id/ib1"
    android:layout_width="fill_parent" <---! The Parent Is The Table Layout !--->
    android:layout_height="wrap_content"
    android:background="@drawable/coolBGImage" />

<TableRow>
BLAH BLAH BLAH
</TableRow>

要将3个ImageButton放在一行,请使用带有android:orientation="horizontal"

的LinearLayout
</TableRow>

<LinearLayout
    android:id="@+id/LL1"
    android:layout_width="fill_parent"  <---! The Parent is the TableLayout !--->
    android:layout_height="wrap_content"
    android:orientation="horizontal">

        <ImageButton
            android:id="@+id/IB1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/coolBGImage1"
            android:weight="1" />

添加相同重量的下两个图像按钮,使按钮均匀分割行。

</LinearLayout>
相关问题