20

我有一个表,每行有 2 行,每行有 3 个按钮。如何使按钮均匀地填充空间。在 HTML 中,我会给它们 33% 的宽度。

您还知道我可以创建一个视图,其中有 4 个连续 4 个图像按钮作为网格布局,类似于启动器。

4

4 回答 4

41

尝试添加android:stretchColumns="*"到您的<TableLayout>标签。

于 2010-02-10T15:09:37.200 回答
17

我终于在这里找到了真正的答案:http ://androidadvice.blogspot.com/2010/10/tablelayout-columns-equal-width.html

这里还有一个关于stackoverflow的更小的例子:https ://stackoverflow.com/a/2865558/265521

例子:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent">
    <TableRow>
        <Button
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:text="Why is doing layouts on Android"
            android:layout_weight="1"
            />
        <Button
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:text="so"
            android:layout_weight="1"
            />
    </TableRow>
    <TableRow>
        <Button
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:text="damn"
            android:layout_weight="1"
            />
        <Button
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:text="frustrating?"
            android:layout_weight="1"
            />
    </TableRow>
</TableLayout>
于 2012-01-11T21:06:02.203 回答
3

将 TableRow layout_width 设置为 fill_parent 并在每个按钮上设置 layout_weight 为 1。

layout_weight 有点像百分比。如果您的所有项目都获得相同的编号,则它们占用相同百分比的空间。如果一个按钮的权重为 2,另一个按钮的权重为 1,那么第一个按钮将占用两倍的空间。

如果您还没有这样做,请准备好通过开发指南的通用布局页面了解布局的良好介绍。

于 2010-02-10T17:03:19.147 回答
0

因此,在结束帖子时,正确的方法是设置NumberOfTheColumns

   <tablelayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:stretchColumns="NumberOfTheColumns"/>

您可以设置需要多少水平“片”。如果您有一个 3x3 表:

 <TableLayout
    android:minWidth="25px"
    android:minHeight="25px"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/tableLayout1"
    android:stretchColumns="3">
    <TableRow
        android:id="@+id/tableRow1">
        <TextView
            android:text="@string/EventTitle"
            android:id="@+id/textView1"
            android:gravity="center_vertical"
            android:layout_column="0" />
        <EditText
            android:inputType="text"
            android:id="@+id/editText3"
            android:layout_weight="2"
            android:layout_span="2"
            android:layout_column="1" />
    </TableRow>
    <TableRow
        android:id="@+id/tableRow2">
        <TextView
            android:text="@string/EventDateStart"
            android:id="@+id/textView1"
            android:gravity="center_vertical"
            android:layout_column="0" />
        <EditText
            android:inputType="date"
            android:id="@+id/editText1"
            android:layout_weight="1"
            android:layout_column="1"
            android:layout_width="wrap_content" />
        <EditText
            android:inputType="time"
            android:id="@+id/editText2"
            android:layout_weight="1"
            android:layout_column="2"
            android:layout_width="match_parent" />
    </TableRow>
    <TableRow
        android:id="@+id/tableRow3">
        <TextView
            android:text="@string/EventDateEnd"
            android:id="@+id/textView1"
            android:gravity="center_vertical"
            android:layout_column="0"
            android:layout_width="match_parent" />
        <EditText
            android:inputType="date"
            android:id="@+id/editText1"
            android:layout_weight="1"
            android:layout_column="1" />
        <EditText
            android:inputType="time"
            android:id="@+id/editText2"
            android:layout_weight="1"
            android:layout_column="2" />
    </TableRow>
    <TableRow>
        <TextView
            android:text="@string/Description"
            android:id="@+id/textView1"
            android:gravity="center_vertical"
            android:layout_weight="3"
            android:layout_span="3"
            android:layout_column="0"
            android:layout_width="match_parent" />
    </TableRow>
    <TableRow>
        <EditText
            android:inputType="textMultiLine"
            android:id="@+id/editText3"
            android:layout_height="50dp"
            android:layout_weight="3"
            android:layout_span="3"
            android:layout_column="0"
            android:layout_width="match_parent" />
    </TableRow>
</TableLayout>

在此示例中,您可以看到一个 5x3 表。您可以通过设置需要多少个完整表格宽度来设置每个列的宽度...

于 2015-03-05T09:53:24.473 回答