android“layout_alignParentBottom”的相对布局

时间:2012-03-23 09:54:43

标签: android alignment android-relativelayout

所以,我有这个布局:

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

这就是它的样子:

enter image description here

但是如果我在按钮上添加android:layout_alignParentBottom="true",它的外观如下:

enter image description here

  1. 有人可以解释一下这种行为吗?
  2. 如何在不调整黄色布局的情况下将我的按钮放在底部而不添加数千种布局以进行解决方法?

6 个答案:

答案 0 :(得分:8)

这个解决方案对我有用

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

答案 1 :(得分:1)

这一个快速的一个屏幕

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

答案 2 :(得分:1)

只是为了尝试,你可以加倍布局的最小尺寸并再试一次吗? 或者你可以设置一个修复布局的高度,并在需要时动态地从代码中更改它。

答案 3 :(得分:0)

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

试试这段代码

答案 4 :(得分:0)

不幸的是,在较新版本的android中仍存在bug。

无论如何,我发现以下可以解决这个问题:

  1. 删除RelativeLayout的minHeight

  2. 在LinearLayout中插入TextView:

    <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. 从Button中删除layout_alignParentBottom并添加layout_alignBottom =“@ id / tvLayout”

  4. 现在LinearLayout“控制”RelativeLayout的高度。 如果TextView高度大于minHeight为100dp,它将会扩展。

    按钮将始终将其底部与LinearLayout对齐,等于RelativeLayout。

答案 5 :(得分:0)

您可以使用ConstraintLayout构建响应式UI

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

结果布局如下所示

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

预览是: enter image description here

在这里阅读更多 -
https://developer.android.com/training/constraint-layout/index.html
https://android-developers.googleblog.com/2017/08/understanding-performance-benefits-of.html

相关问题