How to have a fixed footer with scrollview in android?

☆樱花仙子☆ 提交于 2019-11-28 07:05:11

Seems like your trying to put the footer inside the scroll view with the code you have. If you don't want the footer to move with your scroll content, you need to keep it on the same layer as the scroll view. Put together a simple example of what I believe your looking to accomplish. (didn't use strings.xml or colors.xml in this example but they should be used in for a real project)

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

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_above="@+id/footer"
    android:background="#0000ff"
    android:fillViewport="true" >

    <TextView
        android:id="@+id/texthere"
        android:layout_width="160dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:gravity="center_vertical|center_horizontal"
        android:text="test test test test test test test test test test 
        test test test test test test test test test test test test test 
        test test test test test test test test test test test test test"
        android:textSize="35sp" >
    </TextView>
</ScrollView>

<RelativeLayout
    android:id="@+id/footer"
    android:layout_width="fill_parent"
    android:layout_height="100dp"
    android:background="#ffff00" 
    android:layout_alignParentBottom="true">

    <TextView
        android:id="@+id/texthere2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="things in your footer here"
        android:textSize="20sp" />

</RelativeLayout>

</RelativeLayout>

This solution has only a problem: the footer float over the scroll view and if there is not enough vertical space the footer hide the bottom part of the scroll view.

In order to be sure that the whole scroll view will be always shown I had a bottom padding to the scroll view equals to the footer height in the example (50dp):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
  >
     <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="50dp" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            .......

        </LinearLayout>
    </ScrollView>

    <include
        android:layout_height="50dp"
        android:layout_width="match_parent"
        android:layout_alignParentBottom="true"
        layout="@layout/footer" >
    </include>
  </RelativeLayout>
onkar gupta

You have to make your footerview outside of scrollview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!