Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android "layout_alignParentBottom" in relative layout

So, I have this layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/layout"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="#FFFF00"
                android:minHeight="100dp"
                android:layout_gravity="bottom"
        >
    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:textColor="#000000"
            android:background="#FF0000"
            android:text="Hello World"
            />

    <Button
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_alignParentRight="true"
            android:text="button"/>
</RelativeLayout>

and this is how it looks:

enter image description here

but if I add android:layout_alignParentBottom="true" to the button here is how it looks:

enter image description here

  1. Can someone please explain me this behavior?
  2. How to put my button at the bottom without resizing the yellow layout and without adding thousand of layouts for workarounds?
like image 453
Buda Gavril Avatar asked Mar 23 '12 09:03

Buda Gavril


People also ask

What is layout_alignParentBottom?

android:layout_alignParentBottom : This attribute is used to place the bottom of the element on the bottom of the container. You can change layout_align parent like top, left, and right. android:layout_centerHorizontal : This attribute is used to center the element horizontally within its parent container.

Which has better performance LinearLayout or RelativeLayout?

So Linear Layout should preferred over Relative Layout! Also if you use weight_sum attribute within Linear Layout it would again “measure the child twice”. So avoid using this attribute as much as possible.

What is relative layout in android?

RelativeLayout is a view group that displays child views in relative positions. The position of each view can be specified as relative to sibling elements (such as to the left-of or below another view) or in positions relative to the parent RelativeLayout area (such as aligned to the bottom, left or center).

What is the difference between relative layout and constraint layout?

Unlike RelativeLayout , ConstraintLayout offers a bias value that is used to position a view in terms of 0% and 100% horizontal and vertical offset relative to the handles (marked with a red circle). These percentages (and fractions) offer seamless positioning of the view across different screen densities and sizes.


2 Answers

this solution worked for me

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/layout"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="#FFFF00"
                android:minHeight="100dp"
                android:orientation="horizontal"
                android:layout_gravity="bottom"
        >
    <TextView
            android:id="@+id/tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:textColor="#000000"
            android:background="#FF0000"
            android:text="Hello World"
            />

    <Button
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_gravity="bottom|right"
            android:text="button"/>
</FrameLayout>
like image 111
Buda Gavril Avatar answered Nov 08 '22 08:11

Buda Gavril


this one fast one screen

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="bottom"
    android:background="#FFFF00"
    android:minHeight="100dp" >



    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:text="button" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignTop="@+id/button1"
        android:background="#000000"
        android:text="TextView" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button1"
        android:layout_alignParentLeft="true"
        android:background="#000000"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:orientation="vertical" >

    </LinearLayout>

</RelativeLayout>
like image 34
Android Avatar answered Nov 08 '22 08:11

Android