Android 我对在底部对齐视图有什么误解?

Android 我对在底部对齐视图有什么误解?,android,android-layout,android-relativelayout,Android,Android Layout,Android Relativelayout,我尝试在整个布局的底部对齐线性布局,这是相对的,并在这里找到了解决方案: 但是为什么这也不起作用呢 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" to

我尝试在整个布局的底部对齐线性布局,这是相对的,并在这里找到了解决方案:

但是为什么这也不起作用呢

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.sandbox.activities.ListActivity">

<ListView
    android:id="@+id/my_listview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<LinearLayout
    android:id="@+id/search_footer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/my_listview"
    android:orientation="horizontal"
    android:layout_alignParentBottom="true">

    <EditText
        android:id="@+id/my_edittext"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:hint="Enter Search Term" />

    <Button
        android:id="@+id/find_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Find"
        android:onClick="findThings"/>

</LinearLayout>

如果列表视图很短,比如1行,“search\u footer”就在它的下面,下面有一个巨大的间隙

1) “search_footer”被告知在父对象的底部对齐,父对象是relativelayout。因为relativelayout的高度是match_parent,它是根布局,这意味着屏幕的整个高度,所以为什么不管listview的大小,“search_footer”不在屏幕底部


2) 另外,如果我更改搜索页脚的高度以匹配父项,我希望按钮非常高,但它保持不变。为什么?

您指定了
搜索页脚
与父项底部对齐,但也位于listview下方,因此,在列表较短的情况下,搜索页脚仅在listview和屏幕底部之间的空间中延伸。要指定要与父项底部对齐的页脚,以及要在其上方对齐的listview:

<ListView
    android:id="@+id/my_listview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:layout_above="@+id/search_footer"
    />

<LinearLayout
    android:id="@+id/search_footer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_alignParentBottom="true">


更改
search\u footer
的高度时,按钮不会更改,因为它们被设置为
android:layout\u height=“wrap\u content”
。当您这样做时,您的布局通常会改变,但是在这种情况下,由于与上面相同的原因,布局也不会改变

android:layout_above="@id/search_footer"
移除

android:layout_below="@id/my_listview"

属性来自
LinearLayout

android:layout\u-below=“@id/my\u-listview”
他将在xml文件中得到
循环依赖关系
错误。@Onik我认为OrBar犯了复制粘贴错误,忘记删除对列表视图的引用。关于页脚高度,听起来像是你说的,更深的嵌套布局设置覆盖了它们的父级?虽然我看到您的解决方案适用于页脚,但我仍然不明白为什么说页脚位于listview下方而listview位于页脚上方是不同的;在这两种情况下,父对象都是relativelayout,其高度通过match全屏显示_parent@HukeLau_DABA以下是我的想法。用match_parent attr表示父母的身高会占据整个屏幕的高度,这不是一条可靠的规则。如果元素与高度不匹配怎么办?图形预览如何提前计算元素的高度?我想,这就是你得到的结果的重点。当“说页脚在listview下面”时,如何计算列表的高度?但是当“说listview位于页脚上方”时,您确切地知道它的高度在屏幕顶部和页脚之间…@Onik,是的,这是一个复制粘贴错误。页脚之间的差异位于listview下方,而listview位于页脚上方,这取决于两个页脚中的哪一个先对齐,以及哪一个将基于另一个对齐。基本上,您希望更重要的(布局方面的)锚定到底部,并且listview拉伸以适应其上方的空间,而不是listview占用其所需的空间,而页脚拉伸以适应其所需的区域。