主要活动的内容如下所示:
主屏幕看起来像(所有这些都在 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 许可协议
为了使屏幕中的元素可滚动,我认为您可以在 CoordinatorLayout 中使用 NestedScrollView。我通常将 LinearLayout 与我要在那里显示的所有元素放在一起。
例如:
如果这不起作用,您可以将 ConstraintLayout 替换为 LinearLayout。我希望这有帮助。