Android ScrollView not Scrolling

2019-09-15 10:42发布

问题:

I have this layout for a activity but it does not scroll. How can I find out the solution?

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:layout_marginTop="?attr/actionBarSize">

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="80dp"
            android:orientation="vertical"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            tools:context="com.gurkhatech.schoolmanagementteachers.classrooms.singleclass.students.studentsdetails.StudentDetailsActivity"
            tools:showIn="@layout/activity_student_details">


            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:padding="10dp">

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

                    <ImageView
                        android:id="@+id/image_student_in_content_student_details"
                        android:layout_width="100dp"
                        android:layout_height="100dp"
                        android:layout_gravity="center"
                        android:layout_marginTop="10dp"

                        android:src="@mipmap/ic_launcher"


                        />


                    <TextView
                        android:id="@+id/student_name_in_student_details"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:fontFamily="sans-serif"
                        android:gravity="center"
                        android:text=""
                        android:textColor="@color/colorTeal"


                        android:textSize="16sp" />


                    <TextView
                        android:id="@+id/student_class_in_content_student_details"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_margin="5dp"
                        android:fontFamily="sans-serif"
                        android:gravity="center"
                        android:text=""
                        android:textColor="@color/colorTeal"
                        android:textSize="16sp" />
                </LinearLayout>


            </android.support.v7.widget.CardView>


            <TextView
                android:id="@+id/birthday_in_content_student_details"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:fontFamily="sans-serif-light"
                android:gravity="center"
                android:text=""


                android:textSize="16sp" />


            <TextView
                android:id="@+id/sex_in_content_student_details"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"

                android:fontFamily="sans-serif-light"
                android:gravity="center"
                android:text=""

                android:textSize="16sp" />


            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_margin="15dp"
                android:orientation="horizontal"
                android:weightSum="10">


            </LinearLayout>

            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="5dp"
                android:padding="10dp">


                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:padding="5dp">

                    <TextView
                        android:id="@+id/blood_group_students_details"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:fontFamily="sans-serif-condensed"
                        android:gravity="center"
                        android:text="Blood Group"
                        android:textSize="16sp"

                        android:visibility="gone" />

                    <TextView
                        android:id="@+id/address_students_details"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:fontFamily="sans-serif-light"
                        android:gravity="center"
                        android:text=""

                        android:textSize="16sp" />


                    <TextView
                        android:id="@+id/email_in_content_students_details"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="10dp"
                        android:layout_weight="2"
                        android:fontFamily="sans-serif-light"
                        android:gravity="center"

                        android:text=""
                        android:textColor="@color/colorTeal"
                        android:textSize="16sp" />


                    <TextView
                        android:id="@+id/phone_in_content_students_details"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="10dp"
                        android:layout_weight="2"
                        android:fontFamily="sans-serif-light"
                        android:gravity="center"
                        android:text=""
                        android:textColor="@color/colorTeal"
                        android:textSize="16sp" />


                </LinearLayout>
            </android.support.v7.widget.CardView>


        </LinearLayout>

        <TextView
            android:id="@+id/image_upload_student_details"
            android:layout_width="match_parent"
            android:layout_height="70dp"
            android:text="@string/upload_image"
            android:textColor="@android:color/white"
            android:textSize="30sp"
            android:gravity="center"
            android:layout_alignParentBottom="true"
            android:background="@color/primary" />
    </RelativeLayout>

</ScrollView>

回答1:

You should use fillViewport in your ScrollView Section .

android:orientation="vertical"
android:fillViewport="true"

android:fillViewport

Defines whether the scrollview should stretch its content to fill the viewport.

FYI

Avoid to set large Hard-coded VALUE .android:layout_marginBottom="80dp"



回答2:

Your Updated Code at your point of view -- its working

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize">

<ScrollView
    android:id="@+id/scroll_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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

            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:padding="10dp">

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

                    <ImageView
                        android:id="@+id/image_student_in_content_student_details"
                        android:layout_width="100dp"
                        android:layout_height="100dp"
                        android:layout_gravity="center"
                        android:layout_marginTop="10dp"
                        android:src="@drawable/ic_launcher" />


                    <TextView
                        android:id="@+id/student_name_in_student_details"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:fontFamily="sans-serif"
                        android:gravity="center"
                        android:text=""
                        android:textColor="@color/ans_post_time_color"


                        android:textSize="16sp" />


                    <TextView
                        android:id="@+id/student_class_in_content_student_details"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_margin="5dp"
                        android:fontFamily="sans-serif"
                        android:gravity="center"
                        android:text=""
                        android:textColor="@color/ans_post_time_color"
                        android:textSize="16sp" />
                </LinearLayout>


            </android.support.v7.widget.CardView>


            <TextView
                android:id="@+id/birthday_in_content_student_details"
                android:layout_width="match_parent"
                android:layout_height="300dp"
                android:layout_margin="10dp"
                android:fontFamily="sans-serif-light"
                android:gravity="center"
                android:text="sdf asdf asdf asdf asdf "
                android:textSize="16sp" />

            <TextView
                android:id="@+id/sex_in_content_student_details"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:fontFamily="sans-serif-light"
                android:gravity="center"
                android:text=""

                android:textSize="16sp" />


            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_margin="15dp"
                android:orientation="horizontal"
                android:weightSum="10">


            </LinearLayout>

            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="5dp"
                android:padding="10dp">


                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:padding="5dp">

                    <TextView
                        android:id="@+id/blood_group_students_details"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:fontFamily="sans-serif-condensed"
                        android:gravity="center"
                        android:text="Blood Group"
                        android:textSize="16sp"

                        android:visibility="gone" />

                    <TextView
                        android:id="@+id/address_students_details"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:fontFamily="sans-serif-light"
                        android:gravity="center"
                        android:text=""

                        android:textSize="16sp" />


                    <TextView
                        android:id="@+id/email_in_content_students_details"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="10dp"
                        android:layout_weight="2"
                        android:fontFamily="sans-serif-light"
                        android:gravity="center"

                        android:text=""
                        android:textColor="@color/white"
                        android:textSize="16sp" />


                    <TextView
                        android:id="@+id/phone_in_content_students_details"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="10dp"
                        android:layout_weight="2"
                        android:fontFamily="sans-serif-light"
                        android:gravity="center"
                        android:text=""
                        android:textColor="@color/white"
                        android:textSize="16sp" />


                </LinearLayout>
            </android.support.v7.widget.CardView>


        </LinearLayout>

        <TextView
            android:id="@+id/image_upload_student_details"
            android:layout_width="match_parent"
            android:layout_height="70dp"
            android:layout_alignParentBottom="true"
            android:background="@color/primary"
            android:gravity="center"
            android:text="Upload Image"
            android:textColor="@android:color/white"
            android:textSize="30sp" />
    </RelativeLayout>
</ScrollView>