【发布时间】:2021-10-07 19:33:48
【问题描述】:
我在NestedScrollView 中有ConstraintLayout。
如果我不使用NestedScrollView 包裹ConstraintLayout,那么ConstraintLayout 会占据整个屏幕区域,但是一旦我用NestedScrollView 包裹它,ConstraintLayout 就会包裹它的视图并且只占用包裹的空间。
但是,我需要 ConstraintLayout 与其父 NestedScrollView 或至少在屏幕高度保持一致。
对我来说,我在ViewPager2 中添加了片段。当ConstraintLayout 包装其内容视图时,我无法在运行时看到ViewPager2 中的片段。
如果我删除 NestedScrollView,一切正常,我可以正确查看所有片段。
这是我的布局代码。
<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<TextView
android:id="@+id/textView"
android:layout_width="0dp"
android:text="Sample Text"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
android:layout_marginTop="32dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/textView">
<com.google.android.material.tabs.TabItem
android:id="@+id/first"
android:text="First"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<com.google.android.material.tabs.TabItem
android:id="@+id/second"
android:text="Second"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<com.google.android.material.tabs.TabItem
android:id="@+id/third"
android:text="Third"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</com.google.android.material.tabs.TabLayout>
<androidx.viewpager2.widget.ViewPager2
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/tabLayout"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
我有一些图片。
任何帮助将不胜感激。 谢谢
【问题讨论】:
标签: android android-constraintlayout android-nestedscrollview