【问题标题】:Relative layout placing to other relative layout相对布局放置到其他相对布局
【发布时间】:2025-12-07 23:10:01
【问题描述】:

我正在开发一个聊天应用程序。我想将 edittext 放在底部,然后将消息放在它上面。但是我有一个问题:

我不想将消息放在编辑文本和按钮的顶部。如何防止这种情况发生?这是我的 xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ECECFB"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/message_list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"/>

 <RelativeLayout 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" >


           <EditText
        android:id="@+id/message"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" /> 

               <Button
        android:id="@+id/get_from_user"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        android:text="Gönder" />
    </RelativeLayout>

</RelativeLayout>

我还有一个问题。我怎样才能像其他聊天应用一样做消息列表系统?我的意思是当我发送一条消息时,这条消息放在列表的底部而不是顶部。现在在我的 xml 中它放在顶部。像这样:

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    ListView 设置在EditText 布局之上。 您的布局将如下所示:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ECECFB">
    
     <RelativeLayout 
            android:id="@+id/bottom_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true" >
    
    
            <EditText
            android:id="@+id/message"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" /> 
    
            <Button
            android:id="@+id/get_from_user"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="40dp"
            android:text="Gönder" />
        </RelativeLayout>
    
         <ListView
            android:id="@+id/message_list"
            android:layout_above="@id/bottom_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    
    </RelativeLayout>
    

    对于第二个问题,设置

    android:stackFromBottom="true"
    android:transcriptMode="normal"
    

    ListView

    【讨论】:

      【解决方案2】:

      例如添加

      android:layout_below="@id/message_list"
      

      到您的内部 RelativeLayout 以约束 ListView 和底部 RelativeLayout 不要相互重叠。

      【讨论】:

        【解决方案3】:

        试试这个:

        将此添加到您的相对布局中:android:layout_below="@+id/message_list"

        并将您的列表视图更改为 wrap_content

        【讨论】:

          【解决方案4】:

          基本上是这样做的:

          <?xml version="1.0" encoding="utf-8"?>
          <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:background="#ECECFB"
              android:orientation="vertical" >
          
              <RelativeLayout
                  android:id="@+id/comment_layout"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_alignParentBottom="true"
                  android:layout_alignParentLeft="true" >
          
                  <EditText
                      android:id="@+id/message"
                      android:layout_width="fill_parent"
                      android:layout_height="wrap_content" >
          
                      <requestFocus />
                  </EditText>
          
                  <Button
                      android:id="@+id/get_from_user"
                      android:layout_width="fill_parent"
                      android:layout_height="wrap_content"
                      android:layout_marginTop="40dp"
                      android:text="Gönder" />
              </RelativeLayout>
          
              <ListView
                  android:id="@+id/message_list"
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:layout_above="@+id/comment_layout"
                  android:layout_alignParentLeft="true"
                  android:layout_alignParentTop="true" >
              </ListView>
          
          </RelativeLayout>
          

          【讨论】:

            【解决方案5】:

            尝试改变这个:

            <ListView
                android:id="@+id/message_list"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"/>
            

            到这里:

            <ListView
                android:id="@+id/message_list"
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:layout_weight="1"/>
            

            这应该可以解决问题

            【讨论】:

              【解决方案6】:

              第一个问题: 你需要设置RelativeLayout

              android:bellow="@+id/message_list"
              

              2º 问题: 您需要在 ListView 上设置:

              android:stackFromBottom="true"
              android:transcriptMode="normal"
              

              最终的 XML:

              <?xml version="1.0" encoding="utf-8"?>
              <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:background="#ECECFB"
                  android:orientation="vertical" >
              
                  <ListView
                      android:id="@+id/message_list"
                      android:layout_width="fill_parent"
                      android:layout_height="fill_parent"
                      android:stackFromBottom="true"
                      android:transcriptMode="normal"/>
              
               <RelativeLayout
                      android:bellow="@+id/message_list"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:layout_alignParentBottom="true" >
              
              
                         <EditText
                      android:id="@+id/message"
                      android:layout_width="fill_parent"
                      android:layout_height="wrap_content" /> 
              
                             <Button
                      android:id="@+id/get_from_user"
                      android:layout_width="fill_parent"
                      android:layout_height="wrap_content"
                      android:layout_marginTop="40dp"
                      android:text="Gönder" />
                  </RelativeLayout>
              
              </RelativeLayout>
              

              【讨论】:

              • 我尝试了你的 xml,但我的编辑文本和按钮消失了。它没有显示。
              • 现在测试吧!您需要删除 android:layout_weight="1"
              【解决方案7】:

              添加android:layout_above="@+id/id_of_relative_layout"。 向包含 EditTextRelativeLayout 添加一个 ID。

              【讨论】: