【问题标题】:Andorid: How do I align all the items in Relative Layout to the bottom of the screenAndroid:如何将相对布局中的所有项目对齐到屏幕底部
【发布时间】:2018-02-18 14:10:52
【问题描述】:

我在相对布局中放置了一个线性布局,我正在尝试将父相对布局与屏幕底部对齐。

<RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom">
        <TextView
            android:id="@+id/fruits"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Fruits"
            android:layout_centerHorizontal="true"
            />
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="16sp"
            android:layout_alignParentBottom="true"
            android:gravity="bottom">
            <TextView
                android:id="@+id/message"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:text="Message: " />
            <EditText
                android:id="@+id/message_text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

        </LinearLayout>
    </RelativeLayout>

我基本上试图将文本居中,并且在其下方有一个文本和编辑文本并将它们全部对齐到屏幕底部

            Fruits
Message: ___________________________

【问题讨论】:

  • 你能画一张图来清楚你想要的东西在哪里吗!?
  • 我希望整个东西都在屏幕底部

标签: android android-layout android-linearlayout android-relativelayout


【解决方案1】:

使用android:layout_alignParentBottom="true"检查在您的项目中运行以下解决方案。

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true" >
    <TextView
        android:id="@+id/fruits"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Fruits"
        android:layout_alignTop="@+id/linearLayout2"
        android:layout_centerHorizontal="true" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="16sp"
        android:layout_alignParentBottom="true"
        android:gravity="bottom"
        android:id="@+id/linearLayout2">
        <TextView
            android:id="@+id/message"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="Message: " />
        <EditText
            android:id="@+id/message_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </LinearLayout>
</RelativeLayout>

【讨论】:

  • 我认为该属性适用于 RelativeLayout 的子级,而不是布局本身
  • 要使该属性起作用,它也应该具有相对布局作为其父级。
【解决方案2】:

改变这个:

<RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom">

到这个:

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="bottom">

【讨论】:

    【解决方案3】:

    你需要使用 android:layout_alignParentBottom="true"

     <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">      
    </LinearLayout>
    
    
    <android.support.v7.widget.AppCompatButton
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" // add this to you layout
        android:layout_margin="@dimen/margin_10"
        android:background="@drawable/button_selector"/>
    
       </RelativeLayout>
    

    【讨论】:

      【解决方案4】:

      简单!您必须将 Parent RelativeLayout 的高度 从 wrap_content 更改为 match_parent。这样它将占据整个布局的高度,并将附加到屏幕底部。

      【讨论】:

        【解决方案5】:

        将 layout_above 属性设置为 fruits TextView 到底部 LinearLayout 试试这个

        <RelativeLayout android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        xmlns:android="http://schemas.android.com/apk/res/android">
        <TextView
            android:id="@+id/fruits"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Fruits"
            android:layout_above="@+id/bottomLayout"
            android:layout_centerHorizontal="true"
            />
        <LinearLayout
            android:id="@+id/bottomLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="16sp"
            android:layout_alignParentBottom="true"
            android:gravity="bottom">
            <TextView
                android:id="@+id/message"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:text="Message: " />
            <EditText
                android:id="@+id/message_text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        
        </LinearLayout>
        </RelativeLayout>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-07-08
          • 2021-06-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多