如何垂直滚动 ConstraintLayout?
How can I scroll vertically the ConstraintLayout?
主要内容 activity 如下所示:
主屏幕看起来像(所有这些都在 ConstraintLayout 中):
- 图片标题
- 日期
- 图像本身
- 图片描述
图像描述可能太长而无法显示,为了解决这个问题,我可以将它放在一个 ScrollView 中,这样就可以了。但是,我想 ScrollView 整个 ConstraintLayout,但它不能正常工作:无法滚动并且一些 TextViews 没有出现!
相关代码:
<android.support.constraint.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:id="@+id/desciptionScroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.picture.nasa.nasadailyimage.NasaDailyImage"
tools:showIn="@layout/activity_nasa_daily_image">
<TextView
android:id="@+id/imageTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:text="@string/test_image_title"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/imageDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:text="@string/test_image_date"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageTitle" />
<ImageView
android:id="@+id/imageDisplay"
android:layout_width="368dp"
android:layout_height="408dp"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="3dp"
android:src="@mipmap/test_image"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageDate" />
<TextView
android:id="@+id/imageDescription"
android:layout_width="368dp"
android:layout_height="0dp"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="4dp"
android:text="@string/test_image_description"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageDisplay" />
</android.support.constraint.ConstraintLayout>
要向任何视图添加滚动,请添加 ScrollView。在您的代码中,将其添加为根(如果 ScrolLView 不是根并且有更多内容,请在 ConstraintLayout 周围添加 ScrollView)。将命名空间(包含 xmlns
的行)移动到新根。将 ScrollView 中的宽度和高度添加到 match_parent(或任何你拥有的)并将 ConstraintLayout 的高度设置为 wrap_content.
但是,您将无法在设计模式下正确滚动。 ()。但它仍然会在设备上正常工作。
注意:
如果您的布局不随 SCrollView 滚动,请确保将 ConstraintLayout 的 height 设置为 wrap_content
为了使屏幕中的元素可滚动,我认为您可以在 CoordinatorLayout 中使用 NestedScrollView。
我通常会放置一个 LinearLayout,其中包含我要在那里显示的所有元素。
例如:
<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"
tools:context="com.xengar.android.puzzlewildanimals.ui.HelpActivity">
<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="?attr/colorPrimary"
app:popupTheme="@style/ThemeOverlay.AppCompat.Dark" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<android.support.constraint.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:id="@+id/desciptionScroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.picture.nasa.nasadailyimage.NasaDailyImage"
tools:showIn="@layout/activity_nasa_daily_image">
<TextView
android:id="@+id/imageTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:text="@string/test_image_title"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/imageDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:text="@string/test_image_date"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageTitle" />
<ImageView
android:id="@+id/imageDisplay"
android:layout_width="368dp"
android:layout_height="408dp"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="3dp"
android:src="@mipmap/test_image"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageDate" />
<TextView
android:id="@+id/imageDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="4dp"
android:text="@string/test_image_description"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageDisplay" />
</android.support.constraint.ConstraintLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
如果这不起作用,您可以将 ConstraintLayout 替换为 LinearLayout。
希望对您有所帮助。
被带有 ScrollView 标签的约束布局包围并赋予它 属性 android:isScrollContainer="true"
.
无需使用 NestedScrollView
,只要您可以按照 in this related SO answer
所述,将约束布局 "extend" 超出其 ScrollView 父级
主要内容 activity 如下所示:
主屏幕看起来像(所有这些都在 ConstraintLayout 中):
- 图片标题
- 日期
- 图像本身
- 图片描述
图像描述可能太长而无法显示,为了解决这个问题,我可以将它放在一个 ScrollView 中,这样就可以了。但是,我想 ScrollView 整个 ConstraintLayout,但它不能正常工作:无法滚动并且一些 TextViews 没有出现!
相关代码:
<android.support.constraint.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:id="@+id/desciptionScroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.picture.nasa.nasadailyimage.NasaDailyImage"
tools:showIn="@layout/activity_nasa_daily_image">
<TextView
android:id="@+id/imageTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:text="@string/test_image_title"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/imageDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:text="@string/test_image_date"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageTitle" />
<ImageView
android:id="@+id/imageDisplay"
android:layout_width="368dp"
android:layout_height="408dp"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="3dp"
android:src="@mipmap/test_image"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageDate" />
<TextView
android:id="@+id/imageDescription"
android:layout_width="368dp"
android:layout_height="0dp"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="4dp"
android:text="@string/test_image_description"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageDisplay" />
</android.support.constraint.ConstraintLayout>
要向任何视图添加滚动,请添加 ScrollView。在您的代码中,将其添加为根(如果 ScrolLView 不是根并且有更多内容,请在 ConstraintLayout 周围添加 ScrollView)。将命名空间(包含 xmlns
的行)移动到新根。将 ScrollView 中的宽度和高度添加到 match_parent(或任何你拥有的)并将 ConstraintLayout 的高度设置为 wrap_content.
但是,您将无法在设计模式下正确滚动。 (
注意:
如果您的布局不随 SCrollView 滚动,请确保将 ConstraintLayout 的 height 设置为 wrap_content
为了使屏幕中的元素可滚动,我认为您可以在 CoordinatorLayout 中使用 NestedScrollView。 我通常会放置一个 LinearLayout,其中包含我要在那里显示的所有元素。
例如:
<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"
tools:context="com.xengar.android.puzzlewildanimals.ui.HelpActivity">
<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="?attr/colorPrimary"
app:popupTheme="@style/ThemeOverlay.AppCompat.Dark" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<android.support.constraint.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:id="@+id/desciptionScroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.picture.nasa.nasadailyimage.NasaDailyImage"
tools:showIn="@layout/activity_nasa_daily_image">
<TextView
android:id="@+id/imageTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:text="@string/test_image_title"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/imageDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:text="@string/test_image_date"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageTitle" />
<ImageView
android:id="@+id/imageDisplay"
android:layout_width="368dp"
android:layout_height="408dp"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="3dp"
android:src="@mipmap/test_image"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageDate" />
<TextView
android:id="@+id/imageDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="4dp"
android:text="@string/test_image_description"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageDisplay" />
</android.support.constraint.ConstraintLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
如果这不起作用,您可以将 ConstraintLayout 替换为 LinearLayout。 希望对您有所帮助。
被带有 ScrollView 标签的约束布局包围并赋予它 属性 android:isScrollContainer="true"
.
无需使用 NestedScrollView
,只要您可以按照 in this related SO answer