【问题标题】:android "layout_alignParentBottom" in relative layout相对布局中的android“layout_alignParentBottom”
【发布时间】:2012-04-07 21:20:03
【问题描述】:

所以,我有这样的布局:

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

这就是它的样子:

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

  1. 谁能解释一下这种行为?
  2. 如何在不调整黄色布局大小和不添加数千个布局的情况下将我的按钮放在底部?

【问题讨论】:

    标签: android alignment android-relativelayout


    【解决方案1】:

    您可以使用 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>
    

    预览是:

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

    【讨论】:

      【解决方案2】:

      不幸的是,新版本的 android 中仍然存在错误。

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

      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"

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

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

      【讨论】:

        【解决方案3】:
        <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>
        

        试试这个代码

        【讨论】:

        • 使用你的代码它使我的布局覆盖整个屏幕,因为你已经使用了 android:layout_height="fill_parent" 我希望我的布局保持它在我的第一个屏幕截图中看到的高度跨度>
        • 所以我希望我的布局看起来像我的第一张图片,除了我的按钮应该与底部对齐。
        • 这个你给尺寸的按钮来改变
        【解决方案4】:

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

        【讨论】:

          【解决方案5】:

          这个解决方案对我有用

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

          【讨论】:

            【解决方案6】:

            这一快一屏

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

            【讨论】:

            • 这不起作用,因为它没有考虑到主要相对布局中的 minHeight
            猜你喜欢
            • 1970-01-01
            • 2013-10-09
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-11-05
            相关资源
            最近更新 更多