在屏幕底部固定List View下方的页脚

4
我正在尝试将页脚放在我的列表视图下方。以下是我的操作步骤。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white"
android:orientation="vertical" >

<ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:divider="#BE6D79"
    android:dividerHeight="3dp" />

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="30dp"
    android:background="@color/darkDetna"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/contactBtn"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:layout_weight="1"
        android:background="@drawable/listselector"
        android:paddingRight="0dip"
        android:text="Contact" />

    <Button
        android:id="@+id/faq"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:layout_weight="1"
        android:background="@drawable/listselector"
        android:padding="0dip"
        android:text="Terms And Conditions" />

    <Button
        android:id="@+id/feedback"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:layout_weight="1"
        android:background="@drawable/listselector"
        android:padding="0dip"
        android:text="Feedback  " />

    <Button
        android:id="@+id/fullSIte"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:layout_weight="1"
        android:background="@drawable/listselector"
        android:padding="0dip"
        android:text="Full Site" />
</LinearLayout>
</LinearLayout>

这里

但问题是,如果列表内容很少或只有一个列表项,则页脚将位于列表视图下方。

我希望页脚固定在屏幕底部。

非常感谢您的时间和帮助。

4个回答

9

建议使用 RelativeLayout 布局。

实现方法如下:

  • 添加底部视图并使其与屏幕底部对齐:android:layout_alignParentBottom="true"
  • 在底部布局上方添加列表视图:android:layout_above="@+id/LinearLayout1"
  • 使列表视图填满屏幕:android:layout_width="fill_parent"android:layout_height="fill_parent"

请根据以下代码进行更改:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/white"
    android:orientation="vertical" >

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/LinearLayout1"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_weight="1"
        android:divider="#BE6D79"
        android:dividerHeight="3dp" />

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/LinearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="30dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:background="@color/darkDetna"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/contactBtn"
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:layout_weight="1"
            android:background="@drawable/listselector"
            android:paddingRight="0dip"
            android:text="Contact" />

        <Button
            android:id="@+id/faq"
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:layout_weight="1"
            android:background="@drawable/listselector"
            android:padding="0dip"
            android:text="Terms And Conditions" />

        <Button
            android:id="@+id/feedback"
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:layout_weight="1"
            android:background="@drawable/listselector"
            android:padding="0dip"
            android:text="Feedback  " />

        <Button
            android:id="@+id/fullSIte"
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:layout_weight="1"
            android:background="@drawable/listselector"
            android:padding="0dip"
            android:text="Full Site" />
    </LinearLayout>

</RelativeLayout>

谢谢。我忘记将列表视图设置在底部对齐的视图上方了。 - Bear
@madlymad,如果我们有多个底部视图怎么办?我们如何使其中最下面的视图固定? - Akeshwar Jha
@user5038993,无论您需要使用列表滚动什么,都可以将其添加到列表中或作为列表底部。其他任何内容都可以成为底部对齐布局的一部分。如果您需要更复杂的功能,例如根据用户滚动显示/隐藏页脚,则需要添加自定义代码以获取滚动监听器或查找库。 - madlymad

1
如果你不想使用RelativeLayout,你可以在第二个LinearLayout上设置android:layout_gravity="bottom"

其他的解决方案也不错,但这个对于当前的设计来说是最好的。+1 - Tarik

1
你可以使用RelativeLayout代替LinearLayout,然后使用android:layout_alignParentBottom="true"将视图添加到RelativeLayout的底部。
   <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/white"
    android:orientation="vertical" >

<ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:divider="#BE6D79"
    android:dividerHeight="3dp" />

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="30dp"
    android:background="@color/darkDetna"
    android:orientation="horizontal"
    android:layout_alignParentBottom="true" >

    <Button
        android:id="@+id/contactBtn"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:layout_weight="1"
        android:background="@drawable/listselector"
        android:paddingRight="0dip"
        android:text="Contact" />

    <Button
        android:id="@+id/faq"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:layout_weight="1"
        android:background="@drawable/listselector"
        android:padding="0dip"
        android:text="Terms And Conditions" />

    <Button
        android:id="@+id/feedback"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:layout_weight="1"
        android:background="@drawable/listselector"
        android:padding="0dip"
        android:text="Feedback  " />

    <Button
        android:id="@+id/fullSIte"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:layout_weight="1"
        android:background="@drawable/listselector"
        android:padding="0dip"
        android:text="Full Site" />
</LinearLayout>

0

您可以将RelativeLayout用作您的布局的根视图。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接