Search code examples
androidandroid-layoutandroid-relativelayout

how to align controls left, right and center in relative layout in android


I have 3 controls which I want one aligned left, the other centered, and the last all the way to the right in a relative layout.

I have tried that with the following:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="match_parent">

    <RelativeLayout android:id="@+id/RelativeLayout01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        xmlns:android="http://schemas.android.com/apk/res/android">


        <ImageView
            android:id="@+id/backimg"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/back"
            android:background="@null"
            android:layout_alignParentLeft="true"

            />

        <ImageView
            android:id="@+id/logoimg"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/logo"
            android:background="@null"
            android:layout_centerHorizontal="true"
            />

        <ImageButton
            android:id="@+id/closebtn"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/close"
            android:background="@null"
            android:layout_alignParentRight="true"
            />

    </RelativeLayout>



    <WebView  xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/webview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        />

</LinearLayout>

This stacks all of the controls on top of each other.

How can I get this to look like this:

enter image description here


Solution

  • change the imageViews width to wrap_content and everything will be as you want here is your new code

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="match_parent">
    
    <RelativeLayout android:id="@+id/RelativeLayout01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        xmlns:android="http://schemas.android.com/apk/res/android">
    
    
        <ImageView
            android:id="@+id/backimg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/back"
            android:background="@null"
            android:layout_alignParentLeft="true"
    
            />
    
        <ImageView
            android:id="@+id/logoimg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/logo"
            android:background="@null"
            android:layout_centerHorizontal="true"
            />
    
        <ImageButton
            android:id="@+id/closebtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/close"
            android:background="@null"
            android:layout_alignParentRight="true"
            />
    
    </RelativeLayout>
    
    
    
    <WebView  xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/webview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        />