Reputation: 886
I'm trying to create a grid layout that works on both landscape and portrait on a tablet. I only have 2 columns but i'm running into a problem setting the width for the child.
Is set the GridLayout
to match_parent
. I then try to set my TextView
inside the GridLayout
to match_parent
, thinking that it would stretch to fit inside the GridLayout
. But I see that the TextView
's width is over the edge of the screen. (See Screenshot)
If I delete the TextView
in Column 0, it will stretch properly.
Here's my XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/linearLayout3">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/textView17" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_column="7"
android:layout_row="0"
android:orientation="horizontal"
android:layout_rowSpan="21"
android:layout_gravity="center_horizontal|left"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/linearLayout4">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="119dp"
android:orientation="horizontal"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:gravity="center_vertical|center_horizontal">
<ImageButton
android:id="@+id/phonebtn"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_gravity="center"
android:adjustViewBounds="false"
android:background="@drawable/phonebtn_cchange"
android:cropToPadding="true"
android:scaleType="fitXY" />
</LinearLayout>
<GridLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_below="@+id/linearLayout3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/textView127"
android:layout_row="0"
android:layout_column="0" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/textView128"
android:layout_row="0"
android:layout_column="1" />
</GridLayout>
</RelativeLayout>
I assume that i'm setting a weight wrong, or a col span but i'm not sure. Any help would be appreciated.
Upvotes: 0
Views: 841
Reputation: 441
You can fit the child TextView by setting its width as 0dp and its layout gravity params as "fill". So GridLayout will know, that the width of the TextView is whatever the column width.
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="2">
<ImageView
android:layout_height="@dimen/list_item_height"
android:src="@drawable/ic_image"/>
<TextView
android:layout_width="0dp"
android:layout_gravity="fill"
android:gravity="center_vertical"/>
Upvotes: 1