Android:如何对齐底部的按钮和上面的listview?

jcl*_*ova 74 layout android listview alignment

我想在列表视图的底部有一个按钮.

如果我使用relativeLayout/FrameLayout,它会对齐,但listView会变得非常紧张.

(在底部的按钮后面)

在此输入图像描述

的FrameLayout:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentBottom="true">
        <Button
            android:id="@+id/btnButton"
            android:text="Hello"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom" />
    </FrameLayout>
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)

RelativeLayout的:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true">
        <Button
            android:id="@+id/btnButton"
            android:text="Hello"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom" />
    </RelativeLayout>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

以上两个代码只能像第一张图像一样工作.我想要的是第二张图片.

有人可以帮忙吗?

谢谢.

Che*_*mon 177

一个FrameLayout宗旨是覆盖在彼此之上的东西.这不是你想要的.

在您的RelativeLayout示例中,您将ListViews的高度和宽度设置MATCH_PARENT为使其占用与其父级相同的空间量,从而占用页面上的所有空间(并覆盖按钮).

尝试类似的东西:

<LinearLayout 
   android:layout_width="match_parent" 
   android:layout_height="match_parent" 
   android:orientation="vertical">
  <ListView 
     android:layout_width="match_parent" 
     android:layout_height="0dip" 
     android:layout_weight="1"/>
  <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="0"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

layout_weight决定了如何使用额外的空间.本Button不想舒展超越它需要的空间,所以它具有0的重量ListView要占用所有的额外空间,所以它具有权重为1.

你可以使用a来完成类似的事情RelativeLayout,但如果它只是这两个项目,那么我认为LinearLayout更简单.

  • 解决了我的问题,但``android:weight`应该是:`android:layout_weight` (5认同)
  • 但是这个解决方案不能处理listview不够长以填满屏幕的整个垂直空间的情况.如果列表视图只填满屏幕的一半,则按钮会出现在列表视图的正下方,因此最终会出现在屏幕中间的某个位置.在这种情况下,我们如何使按钮始终显示在底部? (2认同)
  • 这个答案比我在官方文档中遇到的任何内容都要好,这些内容似乎只是向已经了解它的人解释了这些内容. (2认同)