如何在 android 中的相对布局中将控件左对齐、右对齐和居中对齐

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

我有 3 个控件,我想要一个左对齐,另一个居中,最后一个在相对布局中一直向右对齐。

我试过以下方法:

<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>

这会将所有控件堆叠在一起。

我怎样才能让它看起来像这样:

您可以使用 android:layout_toRightOf 让视图位于另一个视图的右侧

<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_toRightOf="@id/backimg"  
            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_toRightOf="@id/logoimg"
            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>

将 imageViews 宽度更改为 wrap_content,一切都会如您所愿 这是您的新代码

<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"
    />