views:

223

answers:

2

I've a little problem with alignement in LinearLayout.

I'm trying to have the frist two elements with left alignement, and the third at the center of the screen.

Here is my code (cleaned from id, text, src) :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/color_background"
    >

    <LinearLayout         android:orientation="horizontal"
                  android:layout_width="fill_parent" 
                  android:layout_height="wrap_content">
    <ImageView     android:layout_width="wrap_content"
               android:layout_height="wrap_content">
    </ImageView>
    <TextView     android:layout_width="wrap_content"
              android:layout_height="wrap_content"></TextView>
    <TextView     android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_gravity="center_horizontal"></TextView>
    </LinearLayout>
</LinearLayout>

alt text

Here is what I'm trying to do, pink and yellow on left, red in the center

pink = imageview
yellow = 1er texview
red = 2ème textview

Any idea ?

A: 

Use a RelativeLayout instead of a LinearLayout. Have pink just be a normal child. Have yellow use android:layout_toRightOf to put it to the right of pink. Have red use android:layout_centerHorizontal="true".

CommonsWare
A: 

So the code you need to use is the following

<?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">
    <ImageView
        android:layout_width="wrap_content"
        android:id="@+id/image"    
        android:layout_height="wrap_content"/>
    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/image"/>
    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"/>
</RelativeLayout>
JaapH