如何在Android布局中将按钮与EditText的底部对齐?

时间:2014-06-15 01:38:40

标签: android android-layout

使用我的布局,我希望EditText s填充其行上的所有水平空间,但留下右侧按钮的剩余空间。

我还希望按钮与行上EditText的底部对齐。

目前,按钮未与EditText的底部对齐,我想知道如何实现这一目标。

以下是截图:

enter image description here

这是我的XML:

<RelativeLayout   
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="12dp"
    android:layout_marginRight="12dp" > 

    <LinearLayout
        android:id="@+id/nameLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">          

        <EditText
            android:id="@+id/nameEditText"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="5dp"
            android:ems="8"
            android:textSize="15sp"
            android:hint="enter name" />

        <ImageButton
            android:id="@+id/micNameButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@android:color/transparent"
            android:src="@drawable/ic_mic" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/editLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/nameLayout">       

        <EditText
            android:id="@+id/notesEditText"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:ems="8"
            android:textSize="15sp"
            android:hint="enter notes" />

        <ImageButton
            android:id="@+id/micNotesButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@android:color/transparent"
            android:src="@drawable/ic_mic" />

    </LinearLayout>

</RelativeLayout>

1 个答案:

答案 0 :(得分:1)

尝试将margin属性保留在LinearLayout中,而不是在实际的EditText或ImageButton中。并将布局的重力设置为bottom

(编辑:另请注意我已将android:orientation="horizontal"添加到LinearLayouts。当子视图使用layout_weight时,您应该总是有方向。

看起来应该是这样的:

<RelativeLayout   
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="12dp"
    android:layout_marginRight="12dp" > 

    <LinearLayout
        android:id="@+id/nameLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_orientation="horizontal"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="5dp"
        android:gravity="bottom" >

        <EditText
            android:id="@+id/nameEditText"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:ems="8"
            android:textSize="15sp"
            android:hint="enter name" />

        <ImageButton
            android:id="@+id/micNameButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@android:color/transparent"
            android:src="@drawable/ic_mic" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/editLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_orientation="horizontal"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="5dp"
        android:gravity="bottom"
        android:layout_below="@+id/nameLayout">       

        <EditText
            android:id="@+id/notesEditText"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:ems="8"
            android:textSize="15sp"
            android:hint="enter notes" />

        <ImageButton
            android:id="@+id/micNotesButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@android:color/transparent"
            android:src="@drawable/ic_mic" />

    </LinearLayout>

</RelativeLayout>