3

我正在尝试学习如何在 android 中使用表格布局,这是我的代码:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tableLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <!-- 3 columns -->
    <TableRow
        android:id="@+id/tableRow3"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="5dip" >

        <TextView
            android:id="@+id/textView2"
            android:text="Column 1" />

        <Button
            android:id="@+id/button2"
            android:text="Column 2" />

        <Button
            android:id="@+id/button3"
            android:text="Column 3" />
    </TableRow>
</TableLayout>

这是结果:

在此处输入图像描述

我怎样才能使TableRow宽度TableLayout和高度适合TableLayout高度?简而言之,我想让所有具有相同宽度的列和具有相同高度的行填充TableLayout,这可能吗?如何?

4

3 回答 3

12

你需要玩weight价值观。两行的 xml 看起来像这样。-

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tableLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <!-- 3 columns -->

    <TableRow
        android:id="@+id/tableRow3"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:padding="5dip" >

        <TextView
            android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="Column 1" />

        <Button
            android:id="@+id/button2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="Column 2" />

        <Button
            android:id="@+id/button3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="Column 3" />
    </TableRow>

    <TableRow
        android:id="@+id/tableRow3"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:padding="5dip" >

        <TextView
            android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="Column 1" />

        <Button
            android:id="@+id/button2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="Column 2" />

        <Button
            android:id="@+id/button3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="Column 3" />
    </TableRow>

</TableLayout>

这就是结果。-

在此处输入图像描述


作为旁注,请注意fill_parent已弃用,您应该match_parent改用它。

于 2013-11-05T13:46:59.710 回答
2

最快的方法是使用layout_weight表格行中的对象。这是您的 xml 在指定后的外观。

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tableLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <!-- 3 columns -->
    <TableRow
        android:id="@+id/tableRow3"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="5dip" >

        <TextView
            android:id="@+id/textView2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Column 1" />

        <Button
            android:id="@+id/button2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Column 2" />

        <Button
            android:id="@+id/button3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Column 3" />
    </TableRow>
</TableLayout>

使用权重时,您希望将其设置layout_width为 0dp,以便它可以覆盖实际值。这种拆分将使您每个视图占据 TableRow 的 1/3。玩弄重量值,直到你得到你喜欢的东西。

于 2013-11-05T13:47:30.430 回答
-1

添加android:layout_weight="0.3"到孩子的内部TableRow 尝试将您的布局xml替换为此

希望这是你正在寻找的

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tableLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <!-- 3 columns -->
    <TableRow
        android:id="@+id/tableRow3"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:padding="5dip" >

        <TextView
            android:id="@+id/textView2"
            android:layout_weight="0.3"
            android:text="Column 1" />

        <Button
            android:id="@+id/button2"
            android:layout_weight="0.3"
            android:text="Column 2" />

        <Button
            android:id="@+id/button3"
            android:layout_weight="0.3"
            android:text="Column 3" />
    </TableRow>
</TableLayout>
于 2013-11-05T13:47:39.367 回答