【问题标题】:Android ListView Footer View not being placed on the bottom of the screenAndroid ListView 页脚视图未放置在屏幕底部
【发布时间】:2011-03-06 19:52:41
【问题描述】:

我确信我缺少一些简单的东西来实现它,但是我已经遍历了我能想出的所有已知组合来实现我想要做的工作。当 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>

Java

    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 添加为布局中的单独视图

提前致谢!

【问题讨论】:

  • 我很抱歉,但我没有添加足够多的 Java 代码。将 footerView 添加到 listView 后,将运行此代码。 mMenuListView.setAdapter(面板);

标签: android listview footer


【解决方案1】:

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

addFooterView() 不是这样工作的。

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

addFooterView() 也不是这样工作的。

如果你想让它随着列表滚动(即,如果三个列表项占据屏幕的上半部分,页脚就在这三个项的正下方),那么使用addFooterView()...页脚始终随列表滚动。

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

【讨论】:

  • 啊,所以如果内容没有填满屏幕并滚动,就没有办法有条件地将视图锚定到屏幕底部?回到我们去的设计师......太棒了。感谢您的回复。
  • 现在我又读了一遍,我一定不清楚设计师想要什么。他们希望页脚与此列表一起滚动,但需要注意:如果列表(包括页脚)小于屏幕,则强制页脚与屏幕底部对齐。否则,只在最后一个 listView 行之后显示它。
  • @George Walters II:Android 中没有任何东西可以简单地实现这种外观。正如 tlayton 建议的那样,可能有一种方法可以实现类似的外观,但难度很大,并且可能会以 UX 为代价(例如,不再可以通过 D-pad 导航)。
  • 如何制作导航抽屉?
【解决方案2】:

如果其他人正在寻找类似的答案,这就是我最终为解决我遇到的问题所做的事情。由于我想要添加的“页脚”只是一个图像来填充屏幕中的一些空间(当视图很短时),而 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>

希望这对其他人有所帮助!

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

    • 好吧,它应该是语义上列表的页脚,我认为我需要做的是在“页脚”之前动态添加一个视图,这将放置适当的空间来放置图像我们想要的地方。
    • 嗨,我需要帮助在我的列表视图中设置页脚并在我单击页脚中的事件时展开列表,让我指导做同样的事情,请提供我一步一步的过程。跨度>
    【解决方案4】:

    和这个一样

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

    【讨论】:

      【解决方案5】:

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

      基本上您将ListView 设置为wrap_content,然后使用负边距将Button 放置在ListView 底部边距空间中。

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

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

      【讨论】:

        猜你喜欢
        • 2013-08-19
        • 2011-11-24
        • 1970-01-01
        • 1970-01-01
        • 2013-04-29
        • 2017-12-04
        • 2020-11-26
        • 1970-01-01
        • 2013-03-15
        相关资源
        最近更新 更多