如何在android布局中创建固定页脚?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2318775/
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
How to create fixed footer in android layout?
提问by UMAR
I am using following code to display button at the bottom of activity.
我正在使用以下代码在活动底部显示按钮。
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
>
<Button android:id="@+id/btnGetMoreResults"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Get more"
/>
</RelativeLayout>
and listview above it. when i display more data in listview this button pannel is moved down.can any one guide me how can i fix it at the bottom of activity?
和上面的列表视图。当我在列表视图中显示更多数据时,此按钮面板向下移动。有人可以指导我如何在活动底部修复它吗?
any help would be appreciated.
任何帮助,将不胜感激。
采纳答案by tbruyelle
The android:layout_alignParentBottom
attribute has to be declared in an element of the RelativeLayout
not in the RelativeLayout
himself (unless there is another RelativeLayout
as a parent).
该android:layout_alignParentBottom
属性必须在RelativeLayout
not 在RelativeLayout
他自己的元素中声明(除非有另一个RelativeLayout
作为父元素)。
You should do something like this, with the ListView
inside the RelativeLayout
also :
你应该做这样的事情,ListView
里面RelativeLayout
也有:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true">
<ListView ...>
<Button android:id="@+id/btnGetMoreResults"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Get more"
android:layout_alignParentBottom="true" />
</RelativeLayout>
回答by Animesh
The answer selected as correct is faulty, the button will hide the lower part of the list view. The correct way is to declare the button first and position the list above the button.
选择正确的答案有误,该按钮将隐藏列表视图的下部。正确的做法是先声明按钮,然后将列表放在按钮上方。
<Button android:id="@+id/btnGetMoreResults"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Get more"
android:layout_alignParentBottom="true"/>
<ListView
...
android:layout_above="@id/btnGetMoreResults"/>
回答by Victor Ferreira
If you had, for example, all the scrollable elements in a ScrollView, you should do like the following:
例如,如果您拥有ScrollView 中的所有可滚动元素,您应该执行以下操作:
<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"
style="@style/rootElement"
>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<!-- texts, buttons, images and anything that you want to scroll -->
</ScrollView>
<LinearLayout
android:orientation="vertical"
style="@style/footer"
android:id="@+id/footer"
android:layout_alignParentBottom="true"
>
</LinearLayout>
</RelativeLayout>
Note that if you want the footer to be fixed, then you shouldn't put it in the ScrollView, where the scrollable content will be placed. Make it child of RelativeLayout and set layout_alignParentBottom
to true. Maybe you'll need to add a padding at the bottom of the ScrollView in this case (so that the last element do not get hidden by the footer).
请注意,如果您希望页脚被固定,那么您不应该将它放在 ScrollView 中,该位置将放置可滚动内容。使其成为RelativeLayout 的子项并设置layout_alignParentBottom
为true。在这种情况下,您可能需要在 ScrollView 的底部添加一个填充(以便最后一个元素不会被页脚隐藏)。
The idea is similar for elements other than ScrollView
对于元素以外的元素,这个想法是相似的 ScrollView