为什么ScrollView无法滚动?

时间:2018-12-12 08:08:51

标签: android android-layout android-scrollview android-constraintlayout

我有一个片段布局,在下端有一些文本和两个图像。

<?xml version="1.0" encoding="utf-8"?>
<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="match_parent"
    android:orientation="vertical"
    tools:context=".ui.MyFragment">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:fillViewport="true">

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/24dp">

            <TextView
                android:id="@+id/fragment_my_start_header"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Title"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent" />


            <TextView
                android:id="@+id/fragment_my_start_description"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="24dp"
                android:layout_marginTop="24dp"
                android:text="Description"
                app:layout_constraintBottom_toTopOf="@id/fragment_my_start_front"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@id/fragment_my_start_header" />


            <ImageView
                android:id="@+id/fragment_my_start_front"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="24dp"
                android:src="@drawable/myapp_perso_rotated"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toLeftOf="@id/fragment_my_start_back"
                app:layout_constraintTop_toBottomOf="@id/fragment_my_start_description" />

            <ImageView
                android:id="@+id/fragment_my_start_back"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="24dp"
                android:src="@drawable/myapp_perso_back_rotated"
                app:layout_constraintLeft_toRightOf="@id/fragment_my_start_front"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@id/fragment_my_start_description" />


            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="24dp"
                android:text="1. Image"
                app:layout_constraintBottom_toTopOf="@id/fragment_my_start_front"
                app:layout_constraintLeft_toLeftOf="@id/fragment_my_start_front" />


            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="24dp"
                android:text="2. Image"
                app:layout_constraintBottom_toTopOf="@id/fragment_my_start_back"
                app:layout_constraintLeft_toLeftOf="@id/fragment_my_start_back" />

        </android.support.constraint.ConstraintLayout>
    </ScrollView>

    <Button
        android:id="@+id/next_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:text="Next"
        android:textColor="#FFFFFF" />

</LinearLayout>

在底部,我有一个下一个底部。 (在较旧的低分辨率设备上)图像在ScrollViews底部被切除。这不是错误。但我想向上滚动,以便可以看到其余的图像。 ScrollView仅移动了一毫米,我看不到整个ImageView

那我怎样才能使整个ScrollView可滚动?

2 个答案:

答案 0 :(得分:0)

将布局更改为此:

<?xml version="1.0" encoding="utf-8"?>
<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_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.MyFragment">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/next_button"
        android:fillViewport="true">

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="@dimen/24dp">

            <TextView
                android:id="@+id/fragment_my_start_header"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Title"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent" />


            <TextView
                android:id="@+id/fragment_my_start_description"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="24dp"
                android:layout_marginTop="24dp"
                android:text="Description"
                app:layout_constraintBottom_toTopOf="@id/fragment_my_start_front"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@id/fragment_my_start_header" />


            <ImageView
                android:id="@+id/fragment_my_start_front"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="24dp"
                android:src="@drawable/myapp_perso_rotated"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toLeftOf="@id/fragment_my_start_back"
                app:layout_constraintTop_toBottomOf="@id/fragment_my_start_description" />

            <ImageView
                android:id="@+id/fragment_my_start_back"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="24dp"
                android:src="@drawable/myapp_perso_back_rotated"
                app:layout_constraintLeft_toRightOf="@id/fragment_my_start_front"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@id/fragment_my_start_description" />


            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="24dp"
                android:text="1. Image"
                app:layout_constraintBottom_toTopOf="@id/fragment_my_start_front"
                app:layout_constraintLeft_toLeftOf="@id/fragment_my_start_front" />


            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="24dp"
                android:text="2. Image"
                app:layout_constraintBottom_toTopOf="@id/fragment_my_start_back"
                app:layout_constraintLeft_toLeftOf="@id/fragment_my_start_back" />

        </android.support.constraint.ConstraintLayout>
    </ScrollView>

    <Button
        android:id="@+id/next_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:layout_alignParentBottom="true"
        android:text="Next"
        android:textColor="#FFFFFF" />

</RelativeLayout>

答案 1 :(得分:0)

我认为图像的底部被切掉了,因为我们没有尝试将图像附加到其父图像的底部。我尝试将app:layout_constraintBottom_toBottomOf="parent"添加到布局的两个图像中。完成所有这些操作后,我删除了一些不必要的代码,例如merginfillViewport,下面得到了此代码。我没有使用的图像,所以我使用的是this address中的图像(PS,很抱歉,我没有在物理或虚拟设备中进行测试,因为我的电脑和分辨率较低的设备都很慢。)

这是我的最终代码(希望有帮助):

<?xml version="1.0" encoding="utf-8"?>
<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="match_parent"
    android:orientation="vertical"
    tools:context=".activities.HomeActivity">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/tv_myStartHeader_fragment"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:text="@string/title"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />


        <TextView
            android:id="@+id/tv_myStartDes_fragment"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/description"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/tv_myStartHeader_fragment" />


        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="@string/_1_image"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/tv_myStartDes_fragment" />

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:gravity="center"
            android:text="@string/_2_image"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/textView2"
            app:layout_constraintTop_toBottomOf="@+id/tv_myStartDes_fragment" />

        <ImageView
            android:id="@+id/fragment_my_start_front"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@string/todo"
            android:src="@drawable/sample"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView2" />

        <ImageView
            android:id="@+id/fragment_my_start_back"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@string/todo"
            android:src="@drawable/sample"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/fragment_my_start_front"
            app:layout_constraintTop_toBottomOf="@+id/textView" />


    </android.support.constraint.ConstraintLayout>
</ScrollView>

<Button
    android:id="@+id/next_button"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:text="@string/next"
    android:textColor="#FFFFFF" />

</LinearLayout>