Android: 在页眉和页脚之间居中滚动视图
Android: Center a scrollview between header and footer
我正在尝试使页眉和页脚之间的 ScrollView 居中。具体来说,我有一个必须位于顶部的 ImageView,一个位于底部的 RelativeView 和一个居中于两者之间的 ScrollView。当 ScrollView 的内容不大于页脚和页眉之间的 space 时,我希望它垂直居中,如下图所示。我目前的尝试将滚动视图放在正确的位置,但是当内容小于可用内容时,它不会正确地垂直居中 space。
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:acg="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/preloginLogo"
android:layout_width="350dp"
android:layout_height="130dp"
android:src="@drawable/logo"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"/>
<ScrollView
android:layout_width="@dimen/prelogin_main_content_width"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@+id/preloginLogo"
android:layout_above="@+id/preloginTertiaryLinks">
<RelativeLayout
android:id="@+id/mainContent"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</RelativeLayout>
</ScrollView>
<RelativeLayout
android:id="@+id/preloginTertiaryLinks"
android:layout_width="match_parent"
android:layout_height="96dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:paddingTop="5dp">
<View
android:layout_width="match_parent"
android:layout_height="1dip"
android:background="@color/lt_gray"
android:layout_centerVertical="true"
android:layout_alignParentTop="true"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true">
<Button
android:id="@+id/prelogin_guestButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Footer"/>
</LinearLayout>
</RelativeLayout>
您可以在顶部和底部给 ScrollView
一个填充。
<ScrollView
android:layout_width="@dimen/prelogin_main_content_width"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@+id/preloginLogo"
android:layout_paddingTop="12dp"
android:layout_paddingBottom="12dp"
android:layout_above="@+id/preloginTertiaryLinks">
<RelativeLayout
android:id="@+id/mainContent"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</RelativeLayout>
</ScrollView>
这个怎么样:
1:输掉上面和下面
2:设置边距,使页眉和页脚不被覆盖。
<ScrollView
android:layout_width="match_parent"
android:layout_marginTop="130dp"
android:layout_marginBottom="96dp"
android:background="@color/red_danger"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
>
<RelativeLayout
android:id="@+id/mainContent"
android:layout_width="match_parent"
android:layout_height="100dp">
</RelativeLayout>
</ScrollView>
我正在尝试使页眉和页脚之间的 ScrollView 居中。具体来说,我有一个必须位于顶部的 ImageView,一个位于底部的 RelativeView 和一个居中于两者之间的 ScrollView。当 ScrollView 的内容不大于页脚和页眉之间的 space 时,我希望它垂直居中,如下图所示。我目前的尝试将滚动视图放在正确的位置,但是当内容小于可用内容时,它不会正确地垂直居中 space。
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:acg="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/preloginLogo"
android:layout_width="350dp"
android:layout_height="130dp"
android:src="@drawable/logo"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"/>
<ScrollView
android:layout_width="@dimen/prelogin_main_content_width"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@+id/preloginLogo"
android:layout_above="@+id/preloginTertiaryLinks">
<RelativeLayout
android:id="@+id/mainContent"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</RelativeLayout>
</ScrollView>
<RelativeLayout
android:id="@+id/preloginTertiaryLinks"
android:layout_width="match_parent"
android:layout_height="96dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:paddingTop="5dp">
<View
android:layout_width="match_parent"
android:layout_height="1dip"
android:background="@color/lt_gray"
android:layout_centerVertical="true"
android:layout_alignParentTop="true"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true">
<Button
android:id="@+id/prelogin_guestButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Footer"/>
</LinearLayout>
</RelativeLayout>
您可以在顶部和底部给 ScrollView
一个填充。
<ScrollView
android:layout_width="@dimen/prelogin_main_content_width"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@+id/preloginLogo"
android:layout_paddingTop="12dp"
android:layout_paddingBottom="12dp"
android:layout_above="@+id/preloginTertiaryLinks">
<RelativeLayout
android:id="@+id/mainContent"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</RelativeLayout>
</ScrollView>
这个怎么样:
1:输掉上面和下面
2:设置边距,使页眉和页脚不被覆盖。
<ScrollView
android:layout_width="match_parent"
android:layout_marginTop="130dp"
android:layout_marginBottom="96dp"
android:background="@color/red_danger"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
>
<RelativeLayout
android:id="@+id/mainContent"
android:layout_width="match_parent"
android:layout_height="100dp">
</RelativeLayout>
</ScrollView>