'Android XML: Unable to vertically align TextViews inside TableRow despite setting their android:gravity to "center_vertical"
In my Android XML layout, I have a TableLayout that contains three cells of equal width (all have weight 1): the first two cells are TextViews and the last cell is a LinearLayout that has two ImageButton(s). However, right now, as shown in the screenshot below, the TextViews are vertically aligned to the top instead of vertically aligning to the center of the TableRow.
This seems strange, considering that I have specified android:gravity to center_vertical for the TextView(s). Below is my XML:
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="70dp"
android:orientation="horizontal"
android:layout_marginBottom="1dp">
<TextView
android:id="@+id/name_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical"
android:text="Name"
android:textColor="#000000" />
<TextView
android:id="@+id/area_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical"
android:text="Area"
android:textColor="#000000" />
<LinearLayout
android:id="@+id/col3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
<ImageButton
android:minWidth="0dp"
android:minHeight="0dp"
android:layout_width="40dp"
android:layout_height="40dp"
android:adjustViewBounds="true"
android:scaleType="centerInside"
android:src="@drawable/edit_icon_foreground" />
<ImageButton
android:minWidth="0dp"
android:minHeight="0dp"
android:layout_width="40dp"
android:layout_height="40dp"
android:adjustViewBounds="true"
android:scaleType="centerInside"
android:src="@drawable/remove_icon_foreground" />
</LinearLayout>
</TableRow>
Solution 1:[1]
android:gravity affects the contents of the view on which it is applied. That is, for TextView, it aligns the text within the view's bounds.
To align the view itself within the parent, use android:layout_gravity. Or, use android:gravity on the TableRow.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ben P. |

