Android/XML 在 EditText 上方对齐 imageView

Android/XML Aligning imageViews above EditTexts

我只是想在 edittext 上方对齐 imageView。编辑文本对齐到 activity 的底部。我将这两对放在 A 相对布局中,并尝试将图像对齐到上方,但这不起作用。 http://i.imgur.com/PnLZSwK.png

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:orientation="horizontal"
    android:id="@+id/linearLayout">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight=".5">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageView2"
            android:layout_above="@+id/edit1"
            android:layout_gravity="center"
            android:background="@drawable/arrow_down"/>

        <EditText
            android:layout_width="match_parent"
            android:layout_alignRight="@android:id/list"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:id="@+id/edit1"
            android:hint="Your name"
            />
    </RelativeLayout>


<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight=".5">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:layout_above="@+id/edit2"
        android:layout_centerHorizontal="true"
        android:background="@drawable/arrow_down"/>

<EditText
    android:layout_width="match_parent"
    android:layout_alignRight="@android:id/list"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:id="@+id/edit2"
    android:hint="Your number"
    android:inputType="number"
    />
</RelativeLayout>


</LinearLayout>




</RelativeLayout>

您的问题的解决方案是 FrameLayout。在您的代码中 RelativeLayout 使用 FrameLayout。首先将 ImageView 放置在 FrameLayout 中,然后将 EditText 放置在那里。那会解决你的问题。如果我能帮到你,请告诉我。

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:orientation="horizontal"
    android:weightSum="1"
    android:id="@+id/linearLayout">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight=".5">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageView2"
            android:background="@drawable/arrow_down"/>

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/edit1"
            android:hint="Your name"
            />
    </FrameLayout>


<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight=".5">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:background="@drawable/arrow_down"/>

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/edit2"
    android:hint="Your number"
    android:inputType="number"
    />
</FrameLayout>

如果您想将箭头指向 EditText,那么以下布局可能会解决您的问题。为了展示您的想法,我刚刚创建了一个带有一些硬编码值的粗略布局。但是在开发项目时,您应该遵循 Google.

提供的标准指南
    <RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

      <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal"
        android:weightSum="1">

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:layout_weight="0.5">

            <ImageView
                android:id="@+id/imageView1"
                android:src="@drawable/ic_down"
                android:layout_width="48dp"
                android:layout_height="48dp"
                android:layout_centerHorizontal="true"/>

            <EditText
                android:id="@+id/name"
                android:layout_marginLeft="8dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/imageView1"
                android:layout_centerHorizontal="true"
                android:hint="Your Name"/>

        </RelativeLayout>

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:layout_weight="0.5">

            <ImageView
                android:id="@+id/imageView2"
                android:src="@drawable/ic_down"
                android:layout_width="48dp"
                android:layout_height="48dp"
                android:layout_centerHorizontal="true"/>

            <EditText
                android:id="@+id/number"
                android:layout_marginLeft="8dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/imageView2"
                android:layout_centerHorizontal="true"
                android:hint="Your Number"/>

        </RelativeLayout>

    </LinearLayout>

  </RelativeLayout>