Android ListView 页脚视图未放置在屏幕底部
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3093481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android ListView Footer View not being placed on the bottom of the screen
提问by George Walters II
I'm sure that I'm missing something simple to get this implemented, but have run through every known combination I can come up with to get what I'm looking to do working. I'm trying to have a ListView footer sit at the bottom of the screen when the ListView items do not fill the page.
我确信我错过了一些简单的东西来实现这个,但是已经遍历了我能想出的所有已知组合来获得我想要做的工作。当 ListView 项目没有填满页面时,我试图让 ListView 页脚位于屏幕底部。
For example, I have a page with a ListView of three items and a FooterView (using the ListView's addFooterView) I want that footerView to sit at the bottom of the screen. When the ListView contains enough items to scroll the screen, the footerView should sit at the end of the list (not at the bottom of the screen).
例如,我有一个包含三个项目的 ListView 和一个 FooterView(使用 ListView 的 addFooterView)的页面,我希望该 footerView 位于屏幕底部。当 ListView 包含足够的项目来滚动屏幕时,footerView 应该位于列表的末尾(而不是屏幕底部)。
The XML I'm trying to use is listed below. Any and all help is much appreciated!
下面列出了我尝试使用的 XML。非常感谢任何和所有帮助!
Layout.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
<?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
<?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);
The things I've tried so far include:
到目前为止我尝试过的事情包括:
- Adding the footer view as a shown above
- Adding the drawable resource as a background on the ListView (This caused the ListView to not span the entire width of the screen and scroll in an odd manner due to the 9-Patch stretchable regions)
- Adding the cone_footer as a separate view in the Layout
- 添加页脚视图,如上图所示
- 在 ListView 上添加可绘制资源作为背景(这导致 ListView 无法跨越屏幕的整个宽度并由于 9-Patch 可拉伸区域而以奇怪的方式滚动)
- 在布局中添加cone_footer 作为单独的视图
Thanks in advance!
提前致谢!
采纳答案by George Walters II
In case others are looking for a similar answer, here is what I ended up doing to solve the issue I had. Since the "Footer" I was looking to add was merely just an image to fill some space in the screen (for when views were short), and the ListView approach wasn't really what I needed for my implementation, we ended up with this as our XML layout:
如果其他人正在寻找类似的答案,这就是我最终为解决我遇到的问题而做的事情。由于我想要添加的“页脚”只是一个图像来填充屏幕中的一些空间(当视图很短时),而 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>
Hope this helps others!
希望这对其他人有帮助!
回答by CommonsWare
I want that footerView to sit at the bottom of the screen.
我希望页脚视图位于屏幕底部。
That is not how addFooterView()
works.
这不是如何addFooterView()
运作的。
When the ListView contains enough items to scroll the screen, the footerView should sit at the end of the list (not at the bottom of the screen).
当 ListView 包含足够的项目来滚动屏幕时,footerView 应该位于列表的末尾(而不是屏幕底部)。
That too is not how addFooterView()
works.
这也不是如何addFooterView()
运作的。
If you want it to scroll with the list (i.e., if the three list items take up the top half of the screen, the footer is right below the three items), then use addFooterView()
...but then the footer always scrolls with the list.
如果您希望它与列表一起滚动(即,如果三个列表项占据屏幕的上半部分,则页脚就在三个项的正下方),然后使用addFooterView()
...但是页脚始终与列表一起滚动.
If you want it to not scroll with the list, one approach is to use a RelativeLayout
as your parent, anchor your footer to the bottom of the screen, and have the ListView
sit in between the top and the footer...but then the footer never scrolls.
如果您希望它不随列表滚动,一种方法是使用 aRelativeLayout
作为您的父级,将页脚锚定到屏幕底部,并将其ListView
置于顶部和页脚之间……但是页脚永远不会滚动。
回答by Parag Chauhan
Same Like this
同样像这样
<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>
回答by Mario Lenci
here is the margin trick I used to achive that behavior.
这是我用来实现这种行为的保证金技巧。
basically you set the ListView
to wrap_content
, and you place the Button
in the ListView
bottom margin space using negative margin.
基本上,您将 设置ListView
为wrap_content
,然后使用负边距将 放置Button
在ListView
底部边距空间中。
this uses fixed height, but you could also mesure the button at runtime and set the positive and negative margin dinamically.
这使用固定高度,但您也可以在运行时测量按钮并动态设置正负边距。
<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>
回答by tlayton
What you're trying to do is actually rather complicated. You want the "footer" to scroll with the list (sometimes at least), which means it needs to be contained within the ListView layout. But then you also (other times) want the same footer to be placed relative to the screen, or more precisely relative to some parent layout (or parent-parent, etc.) of the ListView, which means the footer needs to not be contained within the ListView. These two scenarios require mutually exclusive placement of the element in question.
你想要做的实际上相当复杂。您希望“页脚”与列表一起滚动(有时至少是),这意味着它需要包含在 ListView 布局中。但是,您也(其他时候)希望相对于屏幕放置相同的页脚,或者更准确地说相对于 ListView 的某些父布局(或父-父等),这意味着不需要包含页脚在列表视图中。这两种情况需要相关元素的互斥放置。
This is not poor design on the SDK's part, though. It seems from your description that you might be thinking about the problem the wrong way. Is this element really a footer, conceptually speaking, if it's not always at the "foot" of the list, but sometimes all the way down at the bottom of the screen? You MIGHT be able to make the sort of layout you want by combining ListViews, ScrollViews, RelativeLayouts, and some android:layout_height trickery. It may be a better move, however, to simply take an alternate approach to your design.
不过,就 SDK 而言,这并不是糟糕的设计。从您的描述看来,您可能以错误的方式思考问题。从概念上讲,这个元素是否真的是页脚,如果它并不总是在列表的“脚”,但有时一直在屏幕底部?您可能能够通过组合 ListViews、ScrollViews、RelativeLayouts 和一些 android:layout_height 技巧来制作您想要的那种布局。然而,简单地对您的设计采取替代方法可能是一个更好的举措。