【发布时间】:2015-04-11 15:26:32
【问题描述】:
我有一个 ScrollView 和 1 个 RelativeLayout,其中有 3 个子 RelativeLayouts:
我将顶部RelativeLayout 设置为alignParentTop="true",
底部RelativeLayout 到alignParentBottom="true"。
中间布局到
android:layout_below="@id/topLayout"
android:layout_above="@+id/bottomLayout"
问题: Scrollview 在小屏幕时不滚动,而是 topLayout 停留在顶部,bottomLayout 停留在底部。中间的布局变小了(甚至丢失了),如下所示:
期望:我希望 topLayout 留在顶部,bottomLayout 留在底部。但是没有空间的时候一定要滚动,所以中间的布局不能丢失。
代码:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
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"
android:background="@color/white"
tools:context="com.jemshit.itu.fragments.TakeAttendanceFragment"
android:fillViewport="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:paddingBottom="@dimen/halfClassic"
android:paddingLeft="@dimen/halfClassic"
android:paddingRight="@dimen/halfClassic"
android:paddingTop="@dimen/halfClassic">
<RelativeLayout
android:id="@+id/layoutWeekChoice"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:padding="0dp"
android:background="@color/white"
android:layout_alignParentTop="true">
<Spinner
android:id="@+id/spinnerWeekChoice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_toLeftOf="@+id/buttonSetDate"
android:layout_toStartOf="@+id/buttonSetDate"
/>
<Button
android:id="@+id/buttonSetDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:maxWidth="150dp"
android:textSize="16sp"
android:text="Set Date"
android:gravity="center"
android:textColor="@color/white"
android:textAllCaps="false"
android:background="@drawable/button_background"
android:paddingLeft="@dimen/halfClassic"
android:paddingRight="@dimen/halfClassic"
android:layout_alignBottom="@+id/spinnerWeekChoice"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/layoutCardRead"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:gravity="center"
android:layout_marginTop="32dp"
android:layout_below="@id/layoutWeekChoice"
android:layout_above="@+id/layoutShowAttendance"
android:layout_centerHorizontal="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageCard"
android:src="@drawable/std_card"
android:layout_alignParentTop="true"
android:layout_marginBottom="16dp"
android:layout_centerHorizontal="true"/>
<TextView
android:id="@+id/textWarningCard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Checking..."
android:textColor="@color/black"
android:textSize="14sp"
android:layout_below="@+id/imageCard"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/layoutShowAttendance"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/halfClassic"
android:background="@color/white"
android:layout_alignParentBottom="true">
<TextView
android:id="@+id/textAttended"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Attended: "
android:textColor="@color/black"
android:layout_alignParentTop="true"
android:textSize="16sp"/>
<TextView
android:id="@+id/textNotAttended"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Not Attended: "
android:textColor="@color/black"
android:textSize="16sp"
android:layout_marginBottom="32dp"
android:layout_below="@+id/textAttended"/>
<Button
android:id="@+id/buttonManualAttendance"
android:layout_width="wrap_content"
android:layout_centerHorizontal="true"
android:layout_height="wrap_content"
android:text="Manual Attendance"
android:gravity="center"
android:textAllCaps="false"
android:textSize="16sp"
android:textColor="@color/white"
android:background="@drawable/button_background"
android:minHeight="50dp"
android:layout_below="@id/textNotAttended"
android:paddingLeft="@dimen/halfClassic"
android:paddingRight="@dimen/halfClassic"/>
</RelativeLayout>
</RelativeLayout>
</ScrollView>
注意:我不想将LinearLayout 与weight="1" 一起使用,这会使我的3 个布局高度相同
编辑:现在ScollView 滚动下面的新代码,但我的底部TextViews 迷路了:
代码更新:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
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"
android:background="@color/white"
tools:context="com.jemshit.itu.fragments.TakeAttendanceFragment"
android:fillViewport="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:paddingBottom="@dimen/halfClassic"
android:paddingLeft="@dimen/halfClassic"
android:paddingRight="@dimen/halfClassic"
android:paddingTop="@dimen/halfClassic">
<RelativeLayout
android:id="@+id/layoutWeekChoice"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:padding="0dp"
android:background="@color/white"
android:layout_alignParentTop="true">
<Spinner
android:id="@+id/spinnerWeekChoice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_toLeftOf="@+id/buttonSetDate"
android:layout_toStartOf="@+id/buttonSetDate"
/>
<Button
android:id="@+id/buttonSetDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:maxWidth="150dp"
android:textSize="16sp"
android:text="Set Date"
android:gravity="center"
android:textColor="@color/white"
android:textAllCaps="false"
android:background="@drawable/button_background"
android:paddingLeft="@dimen/halfClassic"
android:paddingRight="@dimen/halfClassic"
android:layout_alignBottom="@+id/spinnerWeekChoice"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/layoutCardRead"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:gravity="center"
android:layout_marginTop="32dp"
android:layout_below="@id/layoutWeekChoice"
android:layout_centerHorizontal="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageCard"
android:src="@drawable/std_card"
android:layout_alignParentTop="true"
android:layout_marginBottom="16dp"
android:layout_centerHorizontal="true"/>
<TextView
android:id="@+id/textWarningCard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Checking..."
android:textColor="@color/black"
android:textSize="14sp"
android:layout_below="@+id/imageCard"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/layoutShowAttendance"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/halfClassic"
android:background="@color/white"
android:layout_below="@id/layoutCardRead"
android:layout_alignParentBottom="true">
<TextView
android:id="@+id/textAttended"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Attended: "
android:textColor="@color/black"
android:layout_above="@+id/textNotAttended"
android:textSize="16sp"/>
<TextView
android:id="@+id/textNotAttended"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Not Attended: "
android:textColor="@color/black"
android:textSize="16sp"
android:layout_marginBottom="32dp"
android:layout_above="@+id/buttonManualAttendance" />
<Button
android:id="@+id/buttonManualAttendance"
android:layout_width="wrap_content"
android:layout_centerHorizontal="true"
android:layout_height="wrap_content"
android:text="Manual Attendance"
android:gravity="center"
android:textAllCaps="false"
android:textSize="16sp"
android:textColor="@color/white"
android:background="@drawable/button_background"
android:minHeight="50dp"
android:layout_alignParentBottom="true"
android:paddingLeft="@dimen/halfClassic"
android:paddingRight="@dimen/halfClassic"/>
</RelativeLayout>
</RelativeLayout>
</ScrollView>
【问题讨论】:
标签: android android-layout android-scrollview android-scroll