Android中ScrollView内ViewPager2无法填充剩余空间求助
解决ScrollView内ViewPager2无法填充剩余空间的问题
问题根源确实是ScrollView内部的ConstraintLayout高度设为wrap_content,导致它没有被拉伸到ScrollView的视口高度,ViewPager2的0dp约束失去了有效的底部参考,只能自适应包裹内容。
修复步骤:
- 给ScrollView添加
android:fillViewport="true",强制子View填充视口高度 - 将ScrollView内部的ConstraintLayout的
android:layout_height从wrap_content改为match_parent - 保留ViewPager2的
0dp高度及上下约束,使其填充TabLayout到父布局底部的剩余空间
修改后的完整布局代码:
<?xml version="1.0" encoding="utf-8"?> <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" android:background="@color/black" tools:context=".MainActivity"> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:fillViewport="true" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.5"> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.5"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="@dimen/_20mdp" android:layout_marginTop="@dimen/_30mdp" android:fontFamily="sans-serif-light" android:lineSpacingExtra="20dp" android:text="My\nNotes" android:textColor="@color/brand_color" android:textSize="50dp" android:textStyle="bold" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <com.google.android.material.tabs.TabLayout android:id="@+id/tab_layout" android:layout_width="@dimen/_230mdp" android:layout_height="wrap_content" android:layout_marginStart="@dimen/_20mdp" android:layout_marginTop="@dimen/_30mdp" android:background="@color/black" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/textView" app:tabGravity="center" app:tabIndicator="@drawable/tab_indicator" app:tabIndicatorAnimationMode="elastic" app:tabIndicatorColor="@color/brand_color" app:tabIndicatorFullWidth="true" app:tabIndicatorGravity="stretch" app:tabMode="fixed" app:tabSelectedTextColor="@color/black" app:tabTextColor="@color/white" /> <androidx.viewpager2.widget.ViewPager2 android:id="@+id/view_pager" android:layout_width="match_parent" android:layout_height="0dp" android:layout_marginTop="@dimen/_30mdp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/tab_layout" /> </androidx.constraintlayout.widget.ConstraintLayout> </ScrollView> </androidx.constraintlayout.widget.ConstraintLayout>
原理说明:
android:fillViewport="true"是核心:ScrollView默认只会让子View包裹内容,设置这个属性后,当子View高度小于ScrollView视口时,子View会被拉伸到视口高度;当内容超过视口时,依然保留滚动功能。- 内部ConstraintLayout设为
match_parent后,就有了明确的上下边界,ViewPager2的0dp约束就能正确计算TabLayout到底部的剩余高度,从而填充空间。
内容的提问来源于stack exchange,提问作者Shalen Mathew
相关产品推荐
相关产品推荐

