15

我确信我错过了一些简单的东西来实现它,但是已经通过了我能想出的所有已知组合来得到我想要做的工作。当 ListView 项目未填满页面时,我试图让 ListView 页脚位于屏幕底部。

例如,我有一个包含三个项目的 ListView 和一个 FooterView(使用 ListView 的 addFooterView)的页面,我希望该 footerView 位于屏幕底部。当 ListView 包含足够的项目来滚动屏幕时,footerView 应该位于列表的末尾(而不是位于屏幕的底部)。

下面列出了我尝试使用的 XML。非常感谢任何和所有帮助!

布局.xml

<?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="@+drawable/background">
<ListView
    android:id="@+id/menu" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:listSelector="@android:color/transparent"
    android:divider="@null">
</ListView></RelativeLayout>

ListViewRow.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
style="@style/Nationwide.menu">
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/nav_item"
    style="@style/Nationwide.menu.row">
    <ImageView 
        android:id="@+id/image_icon"
        style="@style/Nationwide.menu.leftIcon" />
    <TextView 
        android:id="@+id/nav_text" 
        style="@style/Nationwide.menu.text" />
    <ImageView
        android:id="@+id/chevron_symbol"
        style="@style/Nationwide.menu.rightIcon" />
</LinearLayout>
<View
    android:layout_height="1dp"
    android:background="@drawable/nav_item_divider_dark" />
<View
    android:layout_height="1dp"
    android:background="@drawable/nav_item_divider_light" /></LinearLayout>

ListViewFooter.xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/footer"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true">
<View 
    android:id="@+id/footer_image"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentBottom="true" /></RelativeLayout>

爪哇

    LayoutInflater inflater = this.getLayoutInflater();
    View footerView = inflater.inflate(R.layout.footer, null);

    footerView.findViewById(R.id.footer_image).setBackgroundDrawable(resources.getDrawable(R.drawable.cone_footer));

    mMenuListView.addFooterView(footerView);

到目前为止我尝试过的事情包括:

  • 如上图所示添加页脚视图
  • 在 ListView 上添加可绘制资源作为背景(由于 9-Patch 可拉伸区域,这导致 ListView 无法跨越屏幕的整个宽度并以奇怪的方式滚动)
  • 将 cone_footer 添加为布局中的单独视图

提前致谢!

4

5 回答 5

16

我希望该 footerView 位于屏幕底部。

这不是如何addFooterView()工作的。

当 ListView 包含足够的项目来滚动屏幕时,footerView 应该位于列表的末尾(而不是位于屏幕的底部)。

这也不是如何addFooterView()工作的。

如果您希望它与列表一起滚动(即,如果三个列表项占据屏幕的上半部分,则页脚就在这三个项的正下方),然后使用addFooterView()...但是页脚总是与列表一起滚动.

如果您希望它不与列表一起滚动,一种方法是使用 aRelativeLayout作为您的父级,将您的页脚锚定到屏幕底部,并让ListView位于顶部和页脚之间......但是页脚永远不会卷轴。

于 2010-06-22T14:21:17.743 回答
3

如果其他人正在寻找类似的答案,这就是我最终为解决我遇到的问题所做的事情。由于我想要添加的“页脚”只是一个图像来填充屏幕中的一些空间(当视图很短时),而 ListView 方法并不是我实现所需要的,我们最终得到了这个作为我们的 XML 布局:

    <?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res/com.example.android.apis"
    style="@style/company.mainContainer">

    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical">
        <!-- enter page content here -->

        <View android:layout_height="0px" android:layout_weight="1"
            android:visibility="invisible" />

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|right"
            android:scaleType="fitXY"
            android:src="@drawable/footer_cone">
        </ImageView>

    </LinearLayout>

</ScrollView>

希望这对其他人有帮助!

于 2010-07-26T17:41:41.840 回答
1

你想要做的实际上是相当复杂的。您希望“页脚”与列表一起滚动(有时至少),这意味着它需要包含在 ListView 布局中。但是你也(其他时候)希望相对于屏幕放置相同的页脚,或者更准确地说是相对于 ListView 的某些父布局(或父-父等),这意味着不需要包含页脚在列表视图中。这两种情况需要相互排斥地放置相关元素。

不过,这并不是 SDK 的糟糕设计。从您的描述看来,您可能以错误的方式思考问题。这个元素真的是一个页脚吗?从概念上讲,如果它不总是在列表的“脚下”,但有时一直在屏幕底部?您可能能够通过组合 ListViews、ScrollViews、RelativeLayouts 和一些 android:layout_height 技巧来制作您想要的布局。然而,简单地对您的设计采取一种替代方法可能是一个更好的举措。

于 2010-06-22T14:36:11.317 回答
1

一样 像这样

<RelativeLayout>
    <include android:id="@+id/header" layout="@layout/header" android:layout_width="fill_parent" android:layout_alignParentTop="true" />

    <include android:id="@+id/footer" layout="@layout/footer" android:layout_width="fill_parent" android:layout_alignParentBottom="true" /> 

    <ListView android:layout_below="@id/header" android:layout_above="@id/footer"/> 
</RelativeLayout>
于 2012-04-15T06:24:44.550 回答
1

这是我用来实现该行为的保证金技巧。

基本上,您将 设置ListViewwrap_content,然后使用负边距将 放置ButtonListView底部边距空间中。

这使用固定高度,但您也可以在运行时测量按钮并动态设置正负边距。

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

<ListView
    android:id="@+id/list_item"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="50dp"
    android:layout_alignParentTop="true" />

<Button
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:layout_below="@+id/list_item"
    android:paddingBottom="@dimen/margin"
    android:layout_topMargin="-50dp"
    android:paddingTop="@dimen/margin"
    android:text="@string/add" />

</RelativeLayout>
于 2014-01-07T09:59:20.557 回答