代码之家  ›  专栏  ›  技术社区  ›  bukk530

Android TableLayout删除填充

  •  1
  • bukk530  · 技术社区  · 10 年前

    我要做的是删除 TableLayout ,并平铺显示子按钮。

    这是 activity_main.xml 密码

    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin"
        tools:context=".Main"
        android:orientation="horizontal"
        android:baselineAligned="false">
    
        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
    
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="New Button"
                android:id="@+id/button1" />
    
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="New Button"
                android:id="@+id/button4" />
        </TableRow>
    
        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
    
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="New Button"
                android:id="@+id/button2" />
    
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="New Button"
                android:id="@+id/button3" />
        </TableRow>
    
    </TableLayout>
    

    所有填充设置为0。

    这就是我得到的:

    这就是我想要得到的:

    1 回复  |  直到 10 年前
        1
  •  0
  •   Onik    10 年前

    二者都 TableLayout TableRow LinearLayouts 。所以使用 weight 属性将提供所需的结果:

    1. 设置 TableLayout's 宽度和高度 match_parent
    2. 设置 layout_height="0dp" layout_weight="0.5" 对于 TableRows 使它们分别适合屏幕高度的50%;
    3. 设置每个 Button's 高度到 匹配_租金 填充行的高度,并设置 layout_width="0dp" layout_weight=“0.5” 以使行的宽度相等。

    布局如下:

    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="0.5">
    
        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.5"
            android:text="New Button"
            android:id="@+id/button1" />
    
        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.5"
            android:text="New Button"
            android:id="@+id/button4" />
    </TableRow>
    
    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="0.5">
    
        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.5"
            android:text="New Button"
            android:id="@+id/button2" />
    
        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.5"
            android:text="New Button"
            android:id="@+id/button3" />
    </TableRow>
    
    </TableLayout>
    

    下面是它的样子:

    enter image description here