Android 垂直 ScrollView 根本不起作用

Android vertical ScrollView doesn't work at all

我一直被这个问题困扰,对 android:layout_height/width 进行了很多调整,但我似乎不明白为什么它不起作用。我查找了更多类似我的问题,但这些问题似乎对我不起作用。

XML:

<android.support.design.widget.CoordinatorLayout 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="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@drawable/gradient_background"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <ScrollView android:fillViewport="true"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:paddingTop="50dp">

    <RelativeLayout 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_marginTop="5dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:context="nl.melvinsterk.android.medicatieapplicatie.TreatmentActivity"
        android:orientation="vertical">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageView2"
            android:src="@drawable/been"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginBottom="20dp"
            android:layout_above="@+id/imageView3"
            android:layout_alignRight="@+id/lblDoctersNotes"
            android:layout_alignEnd="@+id/lblDoctersNotes" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Proxycozynas"
            android:id="@+id/tvTreatmentMedicine"
            android:width="160dp"
            android:layout_above="@+id/tvTreatmentRemainingDays"
            android:layout_alignLeft="@+id/tvTreatmentRemainingDays"
            android:layout_alignStart="@+id/tvTreatmentRemainingDays" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="Nog maar X dagen"
            android:id="@+id/tvTreatmentRemainingDays"
            android:layout_alignBottom="@+id/imageView2"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_marginRight="30dp"
            android:layout_marginEnd="48dp"
            android:layout_marginBottom="77dp"
            android:width="140dp"
            android:layout_marginLeft="16dp" />


        <View
            style="@style/Divider"
            android:id="@+id/viewSeperator"
            android:layout_alignTop="@+id/imageView3"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:onClick="sideEffectsOnClick"
            android:text="Last van bijwerkingen?"
            android:id="@+id/lblSideEffects"
            android:layout_centerVertical="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:textSize="20dp"
            android:layout_alignRight="@+id/tvTreatmentRemainingDays"
            android:layout_alignEnd="@+id/tvTreatmentRemainingDays" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageView3"
            android:src="@mipmap/ic_next"
            android:layout_centerVertical="true"
            android:layout_alignRight="@+id/viewSeperator"
            android:layout_alignEnd="@+id/viewSeperator"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true" />

        <View
            style="@style/Divider"
            android:layout_marginTop="12dp"
            android:id="@+id/viewSeperator2"
            android:layout_below="@+id/lblSideEffects"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:onClick="howToOnClick"
            android:text="Hoe neem ik mijn medicatie?"
            android:id="@+id/tvHowToMedication"
            android:layout_below="@+id/viewSeperator2"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:textSize="20dp"
            android:layout_alignRight="@+id/lblSideEffects"
            android:layout_alignEnd="@+id/lblSideEffects" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageView6"
            android:src="@mipmap/ic_next"
            android:layout_below="@+id/viewSeperator2"
            android:layout_above="@+id/viewSeperator4"
            android:layout_centerInParent="true"
            android:layout_toRightOf="@+id/tvHowToMedication"
            android:layout_toEndOf="@+id/tvHowToMedication" />

        <View
            style="@style/Divider"
            android:layout_marginTop="12dp"
            android:id="@+id/viewSeperator4"
            android:layout_below="@+id/tvHowToMedication"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Doktersnotitie"
            android:id="@+id/lblDoctersNotes"
            android:layout_below="@+id/viewSeperator4"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginTop="12dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Doktersnotitie komt hier"
            android:id="@+id/tvDoctorsnote"
            android:layout_below="@+id/lblDoctersNotes"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:textSize="16dp" />

        <View
            style="@style/Divider"
            android:id="@+id/viewSeperator3"
            android:layout_marginTop="20dp"
            android:layout_below="@+id/tvDoctorsnote"
            android:layout_alignRight="@+id/imageView3"
            android:layout_alignEnd="@+id/imageView3" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Gebruiksaanwijzing"
            android:id="@+id/lblMedicineInstruction"
            android:layout_below="@+id/viewSeperator3"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginTop="12dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Medicijn instructies komen hier"
            android:id="@+id/tvMedicineInstruction"
            android:layout_below="@+id/lblMedicineInstruction"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:textSize="16dp" />

    </RelativeLayout>
    </ScrollView>
</android.support.design.widget.CoordinatorLayout>

您可能想在将命名空间 xmlns:android="http://schemas.android.com/apk/res/android" 添加到 ScrollView 后尝试。

尝试实现那些 TextViews 或以下内容:

<android.support.v4.widget.NestedScrollView
        android:id="@+id/scroll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clipToPadding="false"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"

       <!-- your content -->

</android.support.v4.widget.NestedScrollView>

或者用你的代码,像这样:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@drawable/gradient_background"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:fillViewport="true"
        android:paddingTop="50dp">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

            <ImageView
                android:id="@+id/imageView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_above="@+id/imageView3"
                android:layout_alignEnd="@+id/lblDoctersNotes"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_alignParentTop="true"
                android:layout_alignRight="@+id/lblDoctersNotes"
                android:layout_marginBottom="20dp"
                android:src="@drawable/been" />

            <TextView
                android:id="@+id/tvTreatmentMedicine"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_above="@+id/tvTreatmentRemainingDays"
                android:layout_alignLeft="@+id/tvTreatmentRemainingDays"
                android:layout_alignStart="@+id/tvTreatmentRemainingDays"
                android:width="160dp"
                android:text="Proxycozynas"
                android:textAppearance="?android:attr/textAppearanceLarge" />

            <TextView
                android:id="@+id/tvTreatmentRemainingDays"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBottom="@+id/imageView2"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:layout_marginBottom="77dp"
                android:layout_marginEnd="48dp"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="30dp"
                android:width="140dp"
                android:text="Nog maar X dagen"
                android:textAppearance="?android:attr/textAppearanceSmall" />


            <View
                android:id="@+id/viewSeperator"
                style="@style/Divider"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_alignTop="@+id/imageView3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <TextView
                android:id="@+id/lblSideEffects"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignEnd="@+id/tvTreatmentRemainingDays"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_alignRight="@+id/tvTreatmentRemainingDays"
                android:layout_centerVertical="true"
                android:onClick="sideEffectsOnClick"
                android:text="Last van bijwerkingen?"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:textSize="20dp" />

            <ImageView
                android:id="@+id/imageView3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignEnd="@+id/viewSeperator"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:layout_alignRight="@+id/viewSeperator"
                android:layout_centerVertical="true"
                android:src="@mipmap/ic_next" />

            <View
                android:id="@+id/viewSeperator2"
                style="@style/Divider"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_below="@+id/lblSideEffects"
                android:layout_marginTop="12dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />


            <TextView
                android:id="@+id/tvHowToMedication"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignEnd="@+id/lblSideEffects"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_alignRight="@+id/lblSideEffects"
                android:layout_below="@+id/viewSeperator2"
                android:layout_marginTop="20dp"
                android:onClick="howToOnClick"
                android:text="Hoe neem ik mijn medicatie?"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:textSize="20dp" />

            <ImageView
                android:id="@+id/imageView6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_above="@+id/viewSeperator4"
                android:layout_below="@+id/viewSeperator2"
                android:layout_centerInParent="true"
                android:layout_toEndOf="@+id/tvHowToMedication"
                android:layout_toRightOf="@+id/tvHowToMedication"
                android:src="@mipmap/ic_next" />

            <View
                android:id="@+id/viewSeperator4"
                style="@style/Divider"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_below="@+id/tvHowToMedication"
                android:layout_marginTop="12dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <TextView
                android:id="@+id/lblDoctersNotes"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_below="@+id/viewSeperator4"
                android:layout_marginTop="12dp"
                android:text="Doktersnotitie"
                android:textAppearance="?android:attr/textAppearanceLarge" />

            <TextView
                android:id="@+id/tvDoctorsnote"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_below="@+id/lblDoctersNotes"
                android:text="Doktersnotitie komt hier"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textSize="16dp" />

            <View
                android:id="@+id/viewSeperator3"
                style="@style/Divider"
                android:layout_alignEnd="@+id/imageView3"
                android:layout_alignRight="@+id/imageView3"
                android:layout_below="@+id/tvDoctorsnote"
                android:layout_marginTop="20dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <TextView
                android:id="@+id/lblMedicineInstruction"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_below="@+id/viewSeperator3"
                android:layout_marginTop="12dp"
                android:text="Gebruiksaanwijzing"
                android:textAppearance="?android:attr/textAppearanceLarge" />

            <TextView
                android:id="@+id/tvMedicineInstruction"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_below="@+id/lblMedicineInstruction"
                android:text="Medicijn instructies komen hier"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textSize="16dp" />

        </RelativeLayout>
    </ScrollView>
</android.support.design.widget.CoordinatorLayout>