问题
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:
- Set
TableLayout's
width and height tomatch_parent
- Set
layout_height="0dp"
andlayout_weight="0.5"
for theTableRows
to make them fit 50% of the screen height each; - Set each
Button's
height tomatch_parent
to fill the row's height, and also set itslayout_width="0dp"
andlayout_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