Android:你如何将底部的按钮与上面的listview对齐?

Android:你如何将底部的按钮与上面的listview对齐?,android,listview,layout,alignment,Android,Listview,Layout,Alignment,我想在listview的底部有一个按钮 如果我使用relativeLayout/FrameLayout,它会对齐,但listView会变得非常糟糕 (在底部按钮后面) 框架布局: <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical"

我想在listview的底部有一个按钮

如果我使用relativeLayout/FrameLayout,它会对齐,但listView会变得非常糟糕

(在底部按钮后面)

框架布局:

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

相对长度:

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

以上两个代码的工作方式与第一个图像相同。我想要的是第二张图片

有人能帮忙吗


谢谢。

A
FrameLayout
s的目的是将事物相互叠加。这不是你想要的

RelativeLayout
示例中,您将
ListView
s的高度和宽度设置为
MATCH\u 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>

layout\u weight
指示如何使用额外的空间。
按钮
不希望超出所需空间,因此其权重为0。
ListView
希望占用所有额外的空间,因此它的权重为1


您可以使用
相对布局来完成类似的任务,但如果只是这两项,那么我认为
线性布局更简单。

在您的相对布局中,listview的高度是
匹配父项
,这是填充父项(对于2.1及更旧版本)所以,最好的解决方案是,若你们想使用相对布局,那个么首先声明你们的按钮,然后声明你们的列表视图,让列表视图的位置在你们的按钮id上方,若你们想让按钮始终位于底部,那个么就让它位于底部。。 片段是

<RelativeLayout 
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:id="@+id/rl1"><Button 
 android:layout_width="MATCH_PARENT" 
 android:layout_height="WRAP_CONTENT" 
 /><ListView 
 android:layout_width="MATCH_PARENT" 
 android:layout_height="0" 
 android:layout_above="@id/listview"/></RelativeLayout>

这将阻止列表视图完全显示,并使按钮出现。


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

    <ListView android:id="@+id/ListView01" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1">
    </ListView>

    <FrameLayout android:id="@+id/FrameLayout01" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content">
         <Button android:id="@+id/Button01" 
              android:layout_width="wrap_content" 
              android:layout_height="wrap_content" 
              android:text="button" 
              android:layout_gravity="center_horizontal">
        </Button>
    </FrameLayout>

  </LinearLayout>
这是你要找的设计。
试试看。

@jclova您可以做的另一件事是在相对布局中使用下面的布局=@+id/listviewid

我需要在底部并排使用两个按钮。我使用了水平线性布局,但是为按钮的线性布局分配
android:layout\u height=“0dp”
android:layout\u weight=“0”
无效。仅为按钮的线性布局分配
android:layout\u height=“wrap\u content”
。以下是我的工作布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

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

    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:orientation="horizontal">

        <Button
            android:id="@+id/new_button"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="New" />

        <Button
            android:id="@+id/suggest_button"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Suggest" />

    </LinearLayout>

</LinearLayout>

我使用的是Xamarin Android,我的要求与上面的William T.Mallard完全相同,即在其下方有两个并排按钮的列表视图。 解决方案是,这个答案在Xamarin Studio中不起作用-当我将ListView的高度设置为“0dp”时,ListView就消失了

我的Xamarin Android工作代码如下:

<?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/ListView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:layout_above="@+id/ButtonsLinearLayout" />
<LinearLayout
    android:id="@id/ButtonsLinearLayout"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:orientation="horizontal"
    android:layout_alignParentBottom="true">
    <Button
        android:id="@+id/Button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" />
    <Button
        android:id="@+id/Button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" />
</LinearLayout>
</RelativeLayout>


我将ButtonsLinearLayout对齐到屏幕底部,并将ListView设置为ButtonsLinearLayout上方。

RelativeLayout
将忽略其子项
android:layout\u width
android:layout\u height
属性,如果子项具有正确定义其
值的属性

要在右侧图像(显示按钮上方的列表)上获得结果,您的布局应如下所示:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@android:id/button1"
        android:layout_alignParentTop="true"/>

    <Button
        android:id="@android:id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="@android:string/ok"/>

</RelativeLayout>

关键是在
回收视图中定义
android:layout\u alignParentTop
(定义
top
值)和上面的
android:layout\u
(定义
bottom
值)。这样,
RelativeLayout
将忽略
android:layout\u height=“match\u parent”
,而
RecyclerView
将放置在
按钮的上方


此外,如果您有更复杂的布局,并且仍然需要定义这些值,请确保查看android:layout\u alignWithParentIfMissing

这将是解决此问题的最佳和最简单的解决方案。只需将android:layout_over=“@id/nameOfId”
添加到要相对于该布局向上移动的布局中

<?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"
    tools:context="com.sumeru.commons.activity.CommonDocumentUploadActivity">

    <ListView

        android:id="@+id/documentList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/verifyOtp" />


    <com.sumeru.commons.helper.CustomButton
        android:id="@+id/verifyOtp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="@string/otp_verification" />
</RelativeLayout>


解决了我的问题,但是
android:weight
应该是:
android:layout\u weight
@Mayra您的解决方案非常有效。但是,是否有理由它只适用于
android:orientation=“vertical”
,而不适用于指定的水平方向或无方向?但此解决方案无法处理listview长度不足以填满整个屏幕垂直空间的情况。如果ListVIEW只填充屏幕的一半,那么按钮就出现在ListVIEW下面,因此在屏幕中间的某个地方结束。在这种情况下,我们如何使按钮始终显示在底部?android:layout\u weight=“0dip”不是真的,它应该是android:layout\u weight=“0”不管怎样,这都是一个很好的方法。这个答案比我在官方文档中遇到的任何解释都要好,这些解释似乎只是向已经知道它的人解释了一些东西。(警告:前面是次要的Android术语迂腐。)不推荐使用的术语是“填充父对象”,而不是“匹配父对象”。这对我来说很有意义,因为“填充”表示两个维度,而“匹配”表示“与相同”。