Reputation: 6805
I am trying to create a static GridLayout
with two rows and two columns. In the bottom row, a button should be centered across the two columns. Instead, the button seems to be centered only within the right column:
(I stretched the button's height so the asymmetry would be more obvious.)
I expected these attributes within the Button
element to center it across the two columns:
android:layout_column="0"
android:layout_columnSpan="2"
android:layout_gravity="center_horizontal"
Here is my complete layout:
<?xml version="1.0" encoding="utf-8"?>
<GridLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="2"
android:rowCount="2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="0"
android:layout_column="0"
android:text="Title: " />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_row="0"
android:layout_column="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="@string/search"
android:layout_row="1"
android:layout_column="0"
android:layout_columnSpan="2"
android:layout_gravity="center_horizontal"/>
</GridLayout>
How can I center an element across multiple columns?
Upvotes: 0
Views: 1083
Reputation: 1274
Previous answer looks fine to me, but alternatively you can enclose the button in a linear layout, something like:
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="2"
android:rowCount="2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_row="0"
android:text="Title: " />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_row="0"
android:text="hello world" />
<LinearLayout
android:orientation="vertical"
android:layout_column="0"
android:layout_row="1"
android:layout_columnSpan="2"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="test buttontest buttontest button"
android:layout_gravity="center_horizontal"
android:layout_weight="1" />
</LinearLayout>
</GridLayout>
This should do what you aim to:
Upvotes: 1
Reputation: 707
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="2"
android:rowCount="2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_row="0"
android:text="Title: " />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_row="0"
android:text="hello world" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_columnSpan="2"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_row="1"
android:text="search" /></GridLayout>
Upvotes: 0