Skip to content Skip to sidebar Skip to footer

Android Tablelayout Remove Padding

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

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

<TableLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><TableRowandroid:layout_width="wrap_content"android:layout_height="0dp"android:layout_weight="0.5"><Buttonandroid:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="0.5"android:text="New Button"android:id="@+id/button1" /><Buttonandroid:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="0.5"android:text="New Button"android:id="@+id/button4" /></TableRow><TableRowandroid:layout_width="wrap_content"android:layout_height="0dp"android:layout_weight="0.5"><Buttonandroid:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="0.5"android:text="New Button"android:id="@+id/button2" /><Buttonandroid: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:

enter image description here

Post a Comment for "Android Tablelayout Remove Padding"