Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using layout_alignBaseline and layout_alignBottom in RelativeLayout together

I'm trying to get a layout that looks something like this:

Relative layout example

That is:

  1. TextView aligned (with margins) to parent left and top.
  2. An EditText to the left of the TextView, to the right of the Button and baseline-aligned with the TextView.
  3. A Button aligned (with only a right margin) to the parent right. And here's the broken part: bottom-aligned to the EditText.

For whatever reason it doesn't work. Here's the code I'd expect to work:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="32dp"
        android:layout_marginTop="32dp"
        android:text="Text:" />

    <EditText
        android:id="@+id/edit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/text"
        android:layout_toLeftOf="@+id/button"
        android:layout_toRightOf="@+id/text"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignBottom="@+id/edit"
        android:layout_marginRight="32dp"
        android:text="Ok" />

</RelativeLayout>

That comes out like this:

Broken RelativeLayout

What's going on?

Edit

Sorry I'm not sure why I changed this example, but in my code I'm actually using an ImageButton, not Button, so the solution can't involve aligning with the baseline of the button - the EditText must be aligned with the bottom (or middle if that is possible) of the button.

like image 526
Timmmm Avatar asked Oct 22 '12 11:10

Timmmm


People also ask

Which parent layout will you use if you have to create a page which contains more than 10 visits aligned vertically?

Android Layout - LinearLayout, RelativeLayout.

Which is better relative layout or linear layout?

Relativelayout is more effective than Linearlayout.

Which layout positions child elements in relation to parent and or other views?

RelativeLayout lets child views specify their position relative to the parent view or to each other (specified by ID).

How do I center a relative layout?

If you want to make it center then use android:layout_centerVertical="true" in the TextView. my Relative Layout has fill_parent for both height and width.


2 Answers

Try this..

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<TextView
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="32dp"
    android:layout_marginTop="32dp"
    android:text="Text:" />


<EditText
    android:id="@+id/edit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/text"
    android:layout_toLeftOf="@+id/button"
    android:layout_toRightOf="@+id/text"
    android:ems="10" >

    <requestFocus />
</EditText>

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/edit"
    android:layout_alignBottom="@+id/edit"
    android:layout_alignParentRight="true"
    android:layout_marginRight="30dp"
    android:text="Ok" />

like image 197
Henrik Avatar answered Sep 19 '22 14:09

Henrik


Appears to be a bug affecting alignBaseline being used as an anchor for other children. https://code.google.com/p/android/issues/detail?id=73697&thanks=73697

like image 33
nmr Avatar answered Sep 22 '22 14:09

nmr