Im trying to create a layout containing a 2x2 grid of buttons (4 total). Ive got the following however it only creates the button grid in the top left. I want the button grid to fill the whole screen.
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tableLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<Button
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="@+id/button3"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<Button
android:id="@+id/button4"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</TableRow>
Try this code.
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tableLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<Button
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1" />
<Button
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1" />
</TableRow>
<TableRow
android:id="@+id/tableRow1"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<Button
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1" />
<Button
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1" />
</TableRow>
</TableLayout>
you should use weight for buttons
I suggest you use a simple RelativeLayout without getting into the hassle of using weights (either with LinearLayout, TableLayout or a combination of both). Here is a sample layout showing how you can achieve a 2 X 2 grid that covers the entire screen using just a RelativeLayout, thereby making it more efficient and faster.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<View
android:id="@+id/centerVerticalShim"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_centerVertical="true"
android:visibility="invisible" />
<View
android:id="@+id/centerHorizontalShim"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:visibility="invisible" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/centerVerticalShim"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_toLeftOf="@+id/centerHorizontalShim"
android:background="#42A5F5"
android:gravity="center"
android:text="@string/one"
android:textColor="#FFFFFF" >
</TextView>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/centerVerticalShim"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/centerHorizontalShim"
android:background="#EF5350"
android:gravity="center"
android:text="@string/two"
android:textColor="#FFFFFF" >
</TextView>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_below="@+id/centerVerticalShim"
android:layout_toLeftOf="@+id/centerHorizontalShim"
android:background="#66BB6A"
android:gravity="center"
android:text="@string/three"
android:textColor="#FFFFFF" >
</TextView>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/centerVerticalShim"
android:layout_toRightOf="@+id/centerHorizontalShim"
android:background="#5C6BC0"
android:gravity="center"
android:text="@string/four"
android:textColor="#FFFFFF" >
</TextView></RelativeLayout>
The above layout results in this:
I hope that this is what u are looking for:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tableLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="*" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" >
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<Button
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" >
<Button
android:id="@+id/button3"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<Button
android:id="@+id/button4"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</TableRow>
</TableLayout>
There is one more way. Try this!!
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<GridLayout
android:id="@+id/gridLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="2"
android:rowCount="2">
<Button
android:id="@+id/button1"
android:layout_gravity="fill"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:text="Button 1" />
<Button
android:id="@+id/button2"
android:layout_gravity="fill"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:text="Button 2" />
<Button
android:id="@+id/button3"
android:layout_gravity="fill"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:text="Button 3" />
<Button
android:id="@+id/button4"
android:layout_gravity="fill"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:text="Button 4" />
</GridLayout>
</android.support.constraint.ConstraintLayout>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.