Android TableLayout remove padding

 ̄綄美尐妖づ 提交于 2019-12-11 11:27:39

问题


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

This is the 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>

All the paddings are set to 0.

This is what I get:

And this is what I'm trying to get:


回答1:


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

  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.

Here is the layout:

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



来源:https://stackoverflow.com/questions/25633660/android-tablelayout-remove-padding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!