如何在Android布局中创建固定页脚?

时间:2010-02-23 14:20:50

标签: android layout

我正在使用以下代码显示活动底部的按钮。

<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> 
它上面的

和listview。当我在listview中显示更多数据时,这个按钮面板向下移动。任何人都可以指导我如何在活动底部修复它?

任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:100)

选择为正确的答案有问题,该按钮将隐藏列表视图的下半部分。正确的方法是首先声明按钮并将列表放在按钮上方。

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

答案 1 :(得分:24)

android:layout_alignParentBottom属性必须在RelativeLayout的元素中声明,而不是RelativeLayout本身(除非有另一个RelativeLayout作为父级)。

您应该执行类似的操作,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> 

答案 2 :(得分:8)

例如,如果您有 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>

请注意,如果您想要修剪页脚,则不应将其放在ScrollView中,其中将放置可滚动内容。将它设为RelativeLayout的子项并将layout_alignParentBottom设置为true。在这种情况下,你可能需要在ScrollView的底部添加一个填充(这样最后一个元素不会被页脚隐藏)。

ScrollView

以外的其他元素的想法类似
相关问题