ScrollView not working inside constraint layout
我在ConstraintLayout中有一个scrollview。 但scrollview在ConstraintLayout中不起作用。 我尝试使用NestedScrollView而不是ScrollView,但仍无法正常工作。 ScrollView在LinearLayout或RelativeLayout上工作正常,但在ConstraintLayout中不起作用。我将android:layout_height更改为match_parent和wrap_content,但没有用。 问题是什么?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | <androidx.constraintlayout.widget.ConstraintLayout 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" xmlns:app="http://schemas.android.com/apk/res-auto" > <include android:id="@+id/other_toolbar_xml" layout="@layout/other_toolbar_xml" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" /> <ScrollView android:layout_width="match_parent" android:layout_height="0dp" app:layout_constraintTop_toBottomOf="@id/other_toolbar_xml" android:fillViewport="true" tools:ignore="MissingConstraints" > <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <ImageView android:id="@+id/img_content_xml" android:layout_width="match_parent" android:layout_height="170dp" app:layout_constraintTop_toBottomOf="@id/other_toolbar_xml" android:scaleType="fitXY" tools:ignore="NotSibling" /> <TextView android:id="@+id/title_content_xml" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="16dp" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@id/img_content_xml" android:layout_marginRight="16dp" android:paddingLeft="16dp" android:textDirection="rtl" android:text="title" android:textSize="17sp" android:textColor="#1d1d1d" /> <TextView android:id="@+id/content_content_xml" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@id/title_content_xml" android:layout_marginTop="20dp" android:layout_marginRight="16dp" android:layout_marginEnd="16dp" android:paddingLeft="16dp" android:textDirection="rtl" android:text="content" android:textColor="#1d1d1d" /> <ImageView android:id="@+id/img_date_content_Xml" android:layout_width="18dp" android:layout_height="18dp" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@id/content_content_xml" android:layout_marginTop="20dp" android:layout_marginRight="16dp" android:layout_marginEnd="16dp" android:layout_marginBottom="16dp" android:src="@drawable/date" /> <TextView android:id="@+id/date_content_xml" android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_constraintTop_toBottomOf="@id/content_content_xml" app:layout_constraintRight_toRightOf="parent" app:layout_constraintRight_toLeftOf="@id/img_date_content_Xml" android:layout_marginTop="20dp" android:layout_marginRight="8dp" android:layout_marginEnd="8dp" android:text="date" android:textColor="#1d1d1d" android:layout_marginBottom="16dp" /> <TextView android:id="@+id/subject_content_xml" android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_constraintTop_toBottomOf="@id/content_content_xml" app:layout_constraintLeft_toLeftOf="parent" android:layout_marginTop="20dp" android:layout_marginStart="16dp" android:layout_marginLeft="16dp" android:singleLine="true" android:text="subject" android:textColor="#1d1d1d" /> </androidx.constraintlayout.widget.ConstraintLayout> </ScrollView> </androidx.constraintlayout.widget.ConstraintLayout> |
在
有两种方法:
删除"父约束布局"并使用RelativeLayout,因为仅在两个布局中不需要ConstraintLayout。 (它通常用于复杂视图以使其变得容易)
如果要使用ConstraintLayout,请提供适当的约束。 您错过了左,右,底部约束,如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 | <ScrollView android:layout_width="match_parent" android:layout_height="0dp" android:fillViewport="true" app:layout_constraintTop_toBottomOf="@id/other_toolbar_xml" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent"> //.... </ScrollView> |
Firstly if you are using
constraintlayout as the parent layout so
you need to constrain child views properly.Scrollview that you have defined is not constrained properly that's why scrollview
doesn't work.
这是我的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | <androidx.constraintlayout.widget.ConstraintLayout 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"> <include android:id="@+id/other_toolbar_xml" layout="@layout/toolbar_back" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" /> <ScrollView android:layout_width="0dp" android:layout_height="0dp" android:fillViewport="true" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/other_toolbar_xml" tools:ignore="MissingConstraints"> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/img_content_xml" android:layout_width="match_parent" android:layout_height="170dp" android:scaleType="fitXY" app:layout_constraintTop_toBottomOf="@id/other_toolbar_xml" tools:ignore="NotSibling" /> <TextView android:id="@+id/title_content_xml" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:layout_marginRight="16dp" android:paddingLeft="16dp" android:text="title" android:textColor="#1d1d1d" android:textDirection="rtl" android:textSize="17sp" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@id/img_content_xml" /> <TextView android:id="@+id/content_content_xml" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:layout_marginEnd="16dp" android:layout_marginRight="16dp" android:paddingLeft="16dp" android:text="content" android:textColor="#1d1d1d" android:textDirection="rtl" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@id/title_content_xml" /> <ImageView android:id="@+id/img_date_content_Xml" android:layout_width="18dp" android:layout_height="18dp" android:layout_marginTop="20dp" android:layout_marginEnd="16dp" android:layout_marginRight="16dp" android:layout_marginBottom="16dp" android:src="@drawable/date" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@id/content_content_xml" /> <TextView android:id="@+id/date_content_xml" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:layout_marginEnd="8dp" android:layout_marginRight="8dp" android:layout_marginBottom="16dp" android:text="date" android:textColor="#1d1d1d" app:layout_constraintRight_toLeftOf="@id/img_date_content_Xml" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@id/content_content_xml" /> <TextView android:id="@+id/subject_content_xml" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="16dp" android:layout_marginLeft="16dp" android:layout_marginTop="20dp" android:singleLine="true" android:text="subject" android:textColor="#1d1d1d" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintTop_toBottomOf="@id/content_content_xml" /> </androidx.constraintlayout.widget.ConstraintLayout> </ScrollView> </androidx.constraintlayout.widget.ConstraintLayout> |