user3866063
user3866063

Reputation: 11

2 TextViews filling up remaining space equally

I am trying to make the following layout for a list item in ListView. Ideally I would want a layout with 5 text views arranged as follows: Basically in the upper row I want two TextViews on each end to be fixed width while the other two equally split the remaining space. I have found suggestions on how to make one textview fill remaining space but nothing on how two textviews can evenly split space. The resulting set of five TextViews should fill the Layout item

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:padding="6dip">

    <TextView
        android:id = "@+id/s1"
        android:layout_width="50dip"
        android:layout_height = "50dip"
        android:text="what"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
    android:gravity="left"
        />
    <TextView
        android:id = "@+id/t1"
         android:layout_width="fill_parent"
        android:layout_height = "50dip"
        android:layout_toRightOf="@id/s1"
        android:text="why"
        android:layout_alignParentTop="true"
        />
    <TextView
        android:id = "@+id/t2"
         android:layout_width="fill_parent"
        android:layout_height = "50dip"
        android:layout_toRightOf="@id/t1"
        android:text="why"
        android:layout_alignParentTop="true"
        />
    <TextView
        android:id = "@+id/s2"
         android:layout_width="50dip"
        android:layout_height = "50dip"
        android:layout_toRightOf="@id/t2"
        android:text="what"
        android:gravity="right"
        android:layout_alignParentTop="true"

        />
    <TextView
        android:id = "@+id/bottom"
         android:layout_width="fill_parent"
        android:layout_height = "wrap_content"
        android:layout_below="@id/s1"
        android:text="what"
        android:gravity="center"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"

        />
    </RelativeLayout>

Upvotes: 0

Views: 1132

Answers (2)

Ibrahim Yildirim
Ibrahim Yildirim

Reputation: 2771

Try this

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" 
android:padding="6dip">

<TextView
    android:id = "@+id/s1"
    android:layout_width="50dip"
    android:layout_height = "50dip"
    android:text="what"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:gravity="left" />

<TextView
    android:id = "@+id/s2"
    android:layout_width="50dip"
    android:layout_height = "50dip"
    android:text="what"
    android:gravity="right"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"/>

<LinearLayout
    android:layout_height="50dip"
    android:layout_width="match_parent"
    android:layout_toRightOf="@id/s1"
    android:layout_toLeftOf="@id/s2"
    android:layout_alignParentTop="true"
    android:orientation="horizontal" >

    <TextView
        android:id = "@+id/t1"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height = "match_parent"
        android:text="why"/>

    <TextView
        android:id = "@+id/t2"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height = "match_parent"
        android:text="why" />

</LinearLayout>

<TextView
    android:id = "@+id/bottom"
    android:layout_width="fill_parent"
    android:layout_height = "wrap_content"
    android:layout_below="@id/s1"
    android:text="what"
    android:gravity="center"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true" />
</RelativeLayout>

Upvotes: 0

Marcin Orlowski
Marcin Orlowski

Reputation: 75629

Replace parent RelativeLayout with LinearLayout (or if you can't add additional LinearLayout and put all TextViews into it then add android:layout_weight="1" to all TextViews you want to occupy equal portion of remaining space.

Upvotes: 0

Related Questions