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

UMA*_*MAR 33 layout android

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

<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> 
Run Code Online (Sandbox Code Playgroud)

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

任何帮助,将不胜感激.

小智 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"/>
Run Code Online (Sandbox Code Playgroud)


tbr*_*lle 25

android:layout_alignParentBottom属性必须在RelativeLayoutnot 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> 
Run Code Online (Sandbox Code Playgroud)


Vic*_*ira 9

例如,如果您有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>
Run Code Online (Sandbox Code Playgroud)

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

这个想法与其他元素相似 ScrollView