I'm trying to get a layout that looks something like this:
That is:
TextView
aligned (with margins) to parent left and top.EditText
to the left of the TextView
, to the right of the Button
and baseline-aligned with the TextView
.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:
What's going on?
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.
Android Layout - LinearLayout, RelativeLayout.
Relativelayout is more effective than Linearlayout.
RelativeLayout lets child views specify their position relative to the parent view or to each other (specified by ID).
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.
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" />
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With