user1256477
user1256477

Reputation: 11201

linearlayout element and their position

I have a linearLayout with 2 elements, a textView and a imagebutton, I would like to center in the linearlayout the textView and right-aligment for the imageButto, but both of them are at left.

<LinearLayout 
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="50dip"
    android:background="@color/usualBike"
>    
    <TextView
        android:id="@+id/usesLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/white"
        android:text="@string/tituloUsos"
        android:textSize="30dip"
        android:layout_gravity="center"
        android:textStyle="bold"
    />   

    <ImageButton
        android:id="@+id/disconnect"
        android:background="@null"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:src="@drawable/icon_logout" />

</LinearLayout>

how can I get it?

Upvotes: 0

Views: 2690

Answers (3)

balaji
balaji

Reputation: 1575

Try this:

<?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="fill_parent"
    android:orientation="horizontal" >

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="50dip"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/usesLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:gravity="center"
        android:text="tituloUsos"
        android:textSize="30dip"
        android:textStyle="bold" />

    <ImageButton
        android:id="@+id/disconnect"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginLeft="20dp"
        android:layout_toRightOf="@+id/usesLabel"
        android:background="@null"
        android:gravity="right"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>

</RelativeLayout>

Upvotes: 0

Zombie
Zombie

Reputation: 1963

Are you particular about LinearLayout? if not why don't you try with RelativeLayout like this:

 <RelativeLayout 
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="50dip"
        android:background="@color/usualBike"
    >

        <TextView
            android:id="@+id/usesLabel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:gravity="center"
            android:text="@string/tituloUsos"
            android:textColor="@color/white"
            android:textSize="30dip"
            android:textStyle="bold" />

        <ImageButton
            android:id="@+id/disconnect"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:background="@null"
            android:gravity="right"
            android:src="@drawable/icon_logout" />

    </RelativeLayout>

Upvotes: 3

user1787773
user1787773

Reputation: 908

try layout_gravity instead of gravity

Upvotes: 0

Related Questions