【问题标题】:Android - Removing space between TableLayout RowsAndroid - 删除 TableLayout 行之间的空间
【发布时间】:2021-12-28 03:59:19
【问题描述】:

我想在按钮“0”和按钮“1”之间删除。我的目标是它们之间的空间与按钮“X”相同。 UI result

这些解决方案不起作用:

How to remove space between two rows in Tablelayout?

How to reduce space between two rows of Table Layout for android?

此外,按钮“X”应该跨越两行,只有当我输入更大的 textSize 时才会这样做。

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TableRow>
        
        <TableLayout android:layout_width="0sp"
            android:layout_height="match_parent"
            android:layout_weight="1">
            <TableRow android:layout_width="0sp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:layout_margin="0sp"
                android:padding="0sp">
                <com.google.android.material.button.MaterialButton
                    android:layout_width="0sp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:text="0"
                    android:layout_margin="2sp"
                    />
            </TableRow>
            
            <TableRow
                android:layout_width="0sp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:layout_margin="0sp"
                android:padding="0sp">
                <com.google.android.material.button.MaterialButton
                    android:layout_width="0sp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:text="1"
                    android:layout_margin="2sp"
                    />
            </TableRow>
        </TableLayout>
        
        <TableLayout
            android:layout_width="0sp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_margin="0sp"
            android:padding="0sp">
            <TableRow>
                <com.google.android.material.button.MaterialButton
                    android:layout_width="0sp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:text="X"
                    android:layout_margin="2sp"
                    />
            </TableRow>
        </TableLayout>
        
    </TableRow>
</TableLayout>

提前感谢您的任何想法。

【问题讨论】:

    标签: android xml android-layout android-tablelayout


    【解决方案1】:

    我用嵌套的TableLayout 尝试了您的方法,但未能完全删除空间。按钮属性insetTopinsetBottom 删除了一些空间。您可以使用嵌套较少的布局使用不同的方法:

    <TableLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <Button
                android:id="@+id/button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0.5"
                android:insetBottom="0dp"
                android:text="1" />
    
            <Button
                android:id="@+id/button2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0.5"
                android:insetBottom="0dp"
                android:text="X" />
        </TableRow>
    
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
    
            <Button
                android:id="@+id/button3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0.5"
                android:insetTop="0dp"
                android:text="Button" />
    
            <Space
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0.5" />
        </TableRow>
    </TableLayout>
    

    【讨论】:

    • 非常感谢。有了insetBottominsetTop,我终于明白了。 :)
    【解决方案2】:

    你可以这样做

    <TableLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        
        <TableRow>
    
            <TableLayout
                    android:layout_width="0sp"
                    android:layout_height="match_parent"
                    android:layout_weight="1">
    
                <TableRow
                        android:layout_width="0sp"
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        android:padding="0sp">
    
                    <com.google.android.material.button.MaterialButton
                            android:layout_width="0sp"
                            android:layout_height="match_parent"
                            android:layout_weight="1"
                            android:text="0" />
                </TableRow>
    
                <TableRow
                        android:layout_width="0sp"
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        android:padding="0sp">
    
                    <com.google.android.material.button.MaterialButton
                            android:layout_width="0sp"
                            android:layout_height="match_parent"
                            android:layout_weight="1"
                            android:text="1" />
                </TableRow>
            </TableLayout>
            
            <com.google.android.material.button.MaterialButton
                    android:layout_width="0sp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:text="X" />
    
        </TableRow>
    </TableLayout>
    

    【讨论】:

      猜你喜欢
      • 2013-08-24
      • 1970-01-01
      • 1970-01-01
      • 2012-05-11
      • 2019-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-16
      相关资源
      最近更新 更多