Android的TableLayout去除填充 [英] Android TableLayout remove padding

查看:131
本文介绍了Android的TableLayout去除填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做的是去除所有的空白和边距一个 TableLayout ,并显示儿童按钮平铺。

What I'm trying to do is to remove every padding and margin from a TableLayout, and display the children buttons tiled.

这是 activity_main.xml中 code

<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。

All the paddings are set to 0.

这是我所得到的:

这就是我试图得到:

推荐答案

TableLayout 的TableRow LinearLayouts 。因此,使用 重量 属性会给你想要的结果:

Both TableLayout and TableRow are LinearLayouts. So using the weight attribute will give you the desired result:


  1. 设置 TableLayout的宽度和高度 match_parent

  2. 设置 layout_height =0dp layout_weight =0.5 TableRows 来使它们适合屏幕高度的50%,每;

  3. 每个按钮的高度设置为 match_parent 来填补行的高度,同时还设置了 layout_width =0dp layout_weight =0.5来同样适合行的宽度。

  1. Set TableLayout's width and height to match_parent
  2. Set layout_height="0dp" and layout_weight="0.5" for the TableRows to make them fit 50% of the screen height each;
  3. Set each Button's height to match_parent to fill the row's height, and also set its layout_width="0dp" and layout_weight="0.5" to fit the row's width equally.

下面是布局:

<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>

这里是它的样子:

And here is how it looks like:

这篇关于Android的TableLayout去除填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆