【问题标题】:Remove space of TableLayout column删除 TableLayout 列的空间
【发布时间】:2013-05-28 09:48:10
【问题描述】:

我通过 TableLayout 对齐 9 复选框。我已按 android:stretchColumns 对齐列,但列之间的空间很高。如何修复那个空间。

<TableLayout
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        android:stretchColumns="1" >

                        <TableRow
                            android:id="@+id/tableRow1"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content" >

                            <CheckBox
                                android:id="@+id/cbRestaurant"
                                android:layout_width="0dp"
                                android:layout_height="wrap_content"
                                android:layout_weight="1"
                                android:text="@string/drawer_select_restaurant"
                                android:textSize="14sp" />

                            <CheckBox
                                android:id="@+id/cbHotel"
                                android:layout_width="0dp"
                                android:layout_height="wrap_content"
                                android:layout_weight="1"
                                android:text="@string/drawer_select_hotel"
                                android:textSize="14sp" />

                            <CheckBox
                                android:id="@+id/cbNightLife"
                                android:layout_width="0dp"
                                android:layout_height="wrap_content"
                                android:layout_weight="1"
                                android:text="@string/drawer_select_nightlife"
                                android:textSize="14sp" />
                        </TableRow>

                        <TableRow
                            android:id="@+id/tableRow2"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content" >

                            <CheckBox
                                android:id="@+id/cbShopping"
                                android:layout_width="0dp"
                                android:layout_height="wrap_content"
                                android:layout_weight="1"
                                android:text="@string/drawer_select_shopping"
                                android:textSize="14sp" />

                            <CheckBox
                                android:id="@+id/cbEmbrassies"
                                android:layout_width="0dp"
                                android:layout_height="wrap_content"
                                android:layout_weight="1"
                                android:text="@string/drawer_select_embrassies"
                                android:textSize="14sp" />

                            <CheckBox
                                android:id="@+id/checkBox3"
                                android:layout_width="0dp"
                                android:layout_height="wrap_content"
                                android:layout_weight="1"
                                android:text="@string/drawer_select_attaction"
                                android:textSize="14sp" />
                        </TableRow>

                        <TableRow
                            android:id="@+id/tableRow3"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content" >

                            <CheckBox
                                android:id="@+id/cbShopping1"
                                android:layout_width="0dp"
                                android:layout_height="wrap_content"
                                android:layout_weight="1"
                                android:text="@string/drawer_select_hospital"
                                android:textSize="14sp" />

                            <CheckBox
                                android:id="@+id/cbEmbrassies1"
                                android:layout_width="0dp"
                                android:layout_height="wrap_content"
                                android:layout_weight="1"
                                android:text="@string/drawer_select_bank"
                                android:textSize="14sp" />

                            <CheckBox
                                android:id="@+id/checkBox4"
                                android:layout_width="0dp"
                                android:layout_height="wrap_content"
                                android:layout_weight="1"
                                android:text="@string/drawer_select_police"
                                android:textSize="14sp" />
                        </TableRow>
                    </TableLayout>

我的当前表

我想要的结果

【问题讨论】:

    标签: android android-tablelayout


    【解决方案1】:

    从每个 CheckBox 中删除 android:layout_weight="1" 并设置 android:layout_width="wrap_content"。这让 TableRow 感到困惑。然后,设置 android:stretchColumns="*" 而不是 1。这会将可用宽度分配给所有列。

    【讨论】:

    • 感谢您的回复,但没有任何改变。空间依然存在
    • 这对我有帮助!我所要做的就是从 xml 中删除 android:layout_weight="1" 并且它起作用了。