简体   繁体   中英

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:

在此输入图像描述

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

在此输入图像描述

  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?

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>

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>

just for try, can you double the min size of your layout and try it again? or maybe you can set a fix the hight of the layout and change it dynamically from the code when needed.

Unfortunately bug still exists in newer versions of android.

Anyway, I found that following can solve that problem :

  1. Delete minHeight of RelativeLayout

  2. Insert TextView in a LinearLayout:

     <LinearLayout android:id="@+id/tvLayout" android:layout_alignParentTop="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:minHeight="100dp" android:gravity="top" > <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#FFFF00FF" android:text="Helo" /> </LinearLayout> 
  3. Delete layout_alignParentBottom from Button and add layout_alignBottom="@id/tvLayout"

Now LinearLayout "controls" the height of RelativeLayout. If TextView height is bigger than minHeight of 100dp, it will expand.

And button will always align its bottom to LinearLayout, equal to RelativeLayout one.

You can build a responsive UI with ConstraintLayout

compile 'com.android.support.constraint:constraint-layout:1.0.2'

The result layout will looks like

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/layout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:background="#FFFF00"
    android:minHeight="100dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#FF0000"
        android:text="Hello World"
        android:textColor="#000000"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />
</android.support.constraint.ConstraintLayout>

The preview is: 在此输入图像描述

Read more here -
https://developer.android.com/training/constraint-layout/index.html
https://android-developers.googleblog.com/2017/08/understanding-performance-benefits-of.html

<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" >

    <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_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:text="button" />

</RelativeLayout>

try this code

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM