darlik
darlik

Reputation: 255

Three textView in one row

I'm trying to do 3 TextView in a row. I worked up an example and now I have:

        <RelativeLayout
            android:id="@+id/relative"
            android:layout_height="wrap_content"  
            android:layout_width="fill_parent"
            android:padding="5dp">
                    <TextView
                        android:id="@+id/TextView01"
                        android:layout_width="wrap_content"
                        android:layout_height="?android:attr/listPreferredItemHeight"
                        android:textColor="#f00"
                        android:text="one" 
                        android:layout_alignParentLeft="true" ></TextView>
                    <TextView
                        android:id="@+id/TextView02"
                        android:layout_width="wrap_content"
                        android:layout_height="?android:attr/listPreferredItemHeight"
                        android:textColor="#f00"
                        android:layout_alignParentLeft="true"
                        android:text="two" ></TextView>
                    <TextView
                        android:id="@+id/TextView03"
                        android:layout_width="wrap_content"
                        android:layout_height="?android:attr/listPreferredItemHeight"
                        android:textColor="#f00"
                        android:text="three" 
                        android:layout_alignParentRight="true"></TextView>
        </RelativeLayout>

I would like have: id:TextView01 on the left, id:TextView02 on the middle and id:TextView03 on the right.

Now id:TextView01 and id:TextView03 working good, but id:TextView02 is applied to the id:TextView01.

I can use other layout, but I want to have it centered automatically from left to right.

Upvotes: 1

Views: 1566

Answers (4)

Heisenberg
Heisenberg

Reputation: 3193

Create LinearLayout. Make textviews match parent and equal layout weight to take exactly 1/3 of the row

<LinearLayout
            android:id="@+id/relative"
            android:layout_height="wrap_content"  
            android:layout_width="fill_parent"
            android:padding="5dp"
            orientation="horizontal">
                    <TextView
                        android:id="@+id/TextView01"
                        android:layout_width="match_parent"
                        android:layout_weight="1"
                        android:layout_height="?android:attr/listPreferredItemHeight"
                        android:textColor="#f00"
                        android:text="one" 
 ></TextView>
                    <TextView
                        android:id="@+id/TextView02"
                        android:layout_width="match_parent"
                        android:layout_weight="1"
                        android:layout_height="?android:attr/listPreferredItemHeight"
                        android:textColor="#f00"
                        android:layout_alignParentLeft="true"
                        android:text="two" ></TextView>
                    <TextView
                        android:id="@+id/TextView03"
                        android:layout_width="match_parent"
                        android:layout_weight="1"
                        android:layout_height="?android:attr/listPreferredItemHeight"
                        android:textColor="#f00"
                        android:text="three" 
                        android:layout_alignParentRight="true"></TextView>
        </LinearLayout>

Upvotes: 1

bhargavg
bhargavg

Reputation: 1379

If all you want is a 3 textviews in a line, Then you can put three of them in a LinearLayout and set layout_width=0 and layout_weight=1 to all 3 textviews

Upvotes: 1

Ozan
Ozan

Reputation: 1201

Dont use Relative Layout for this kind of stuffs. Try to Linear Layout.

and your code can be:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1"
android:id="@+id/relative"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp" >

<TextView
    android:id="@+id/TextView01"
    android:layout_width="wrap_content"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:layout_alignParentLeft="true"
    android:layout_weight="1"
    android:text="one"
    android:textColor="#f00" >

</TextView>

<TextView
    android:id="@+id/TextView02"
    android:layout_width="wrap_content"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:layout_alignParentLeft="true"
    android:layout_weight="1"
    android:text="two"
    android:textColor="#f00" >

</TextView>

<TextView
    android:id="@+id/TextView03"
    android:layout_width="wrap_content"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:layout_alignParentRight="true"
    android:layout_weight="1"
    android:text="three"
    android:textColor="#f00" >

</TextView>
</LinearLayout>

Upvotes: 1

nano_nano
nano_nano

Reputation: 12523

work with layout_toRightOf:

       <RelativeLayout
        android:id="@+id/relative"
        android:layout_height="wrap_content"  
        android:layout_width="fill_parent"
        android:padding="5dp">
                <TextView
                    android:id="@+id/TextView01"
                    android:layout_width="wrap_content"
                    android:layout_height="?android:attr/listPreferredItemHeight"
                    android:textColor="#f00"
                    android:text="one" 
                    android:layout_alignParentLeft="true" ></TextView>
                <TextView
                    android:id="@+id/TextView02"
                    android:layout_width="wrap_content"
                    android:layout_height="?android:attr/listPreferredItemHeight"
                    android:textColor="#f00"
                    android:layout_toRightOf="@id/TextView01"
                    android:text="two" ></TextView>
                <TextView
                    android:id="@+id/TextView03"
                    android:layout_width="wrap_content"
                    android:layout_height="?android:attr/listPreferredItemHeight"
                    android:textColor="#f00"
                    android:layout_toRightOf="@id/TextView02"
                    android:text="three" 
                    ></TextView>
    </RelativeLayout>

or with LinearLayout type horizontal.

Upvotes: 1

Related Questions