关于在底部调整观点我有什么误解?

时间:2014-08-17 00:42:39

标签: android android-layout android-relativelayout

我尝试将我的linearlayout对齐在我的整个布局的底部,这是相对的,并找到了解决方案:How to align views at the bottom of the screen?

但为什么这项工作也没有?

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

如果listview结果很短,比如1行,&#34; search_footer&#34;就在它下面,它下面有一个巨大的空隙。

1)&#34; search_footer&#34;被告知在父母的底部对齐,这是relativelayout。由于relativelayout的高度是match_parent,并且它是根布局,这意味着屏幕的整个高度,所以为什么不会搜索&#34; search_footer&#34;无论listview的大小如何,都在屏幕的底部?

2)另外,如果我将search_footer的高度更改为match_parent,我预计会有一个非常高的按钮,但它保持不变 - 为什么?

2 个答案:

答案 0 :(得分:1)

添加到ListView此属性

android:layout_above="@id/search_footer"

并删除

android:layout_below="@id/my_listview"
来自LinearLayout

属性。

答案 1 :(得分:0)

您指定search_footer与父级底部对齐但也位于列表视图下方,因此在列表很短的情况下,搜索页脚只会在列表视图和屏幕底部之间的空间中延伸。您想要指定要与父级底部对齐的页脚,并将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_footer的高度时,按钮不会更改,因为它们设置为android:layout_height="wrap_content"。当你这样做时,你的布局通常会改变,但是在这种情况下它也没有改变,原因与上面相同

相关问题