如何垂直滚动 ConstraintLayout?

新手上路,请多包涵

主要活动的内容如下所示:

主屏幕看起来像(所有这些都在 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>

原文由 user7879812 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.6k
2 个回答

为了使屏幕中的元素可滚动,我认为您可以在 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。我希望这有帮助。

原文由 Angel Garcia 发布,翻译遵循 CC BY-SA 3.0 许可协议

要向任何视图添加滚动,您可以添加一个 ScrollView。在您的代码中,将其添加为根(如果 ScrolLView 不是根并且有更多内容,请在 ConstraintLayout 周围添加 ScrollView)。将命名空间(包含 xmlns 的行)移动到新根。将 ScrollView 中的宽度和高度添加到 match_parent(或任何你拥有的)并将 ConstraintLayout 的高度设置为 wrap_content。

但是,您将无法在设计模式下正确滚动。 ( 参考)。但它仍然可以在设备上正常工作。

笔记:

如果您的布局不随 SCrollView 滚动,请确保将 ConstraintLayout高度 设置为 wrap_content

原文由 Zoe stands with Ukraine 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题