如何正确使用ConstraintLayout?ViewPager2布局覆盖问题求助
Android布局问题:ViewPager2覆盖其他UI元素
布局包含TabLayout、ViewPager2和底部按钮栏,ViewPager2容器会覆盖其他界面元素导致显示异常。需要将ViewPager2放在TabLayout和按钮之间,适配空间且不覆盖其他UI,尝试绑定ViewPager2顶部到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" tools:context=".MainActivity"> <com.google.android.material.tabs.TabLayout android:id="@+id/scheduleDaySelector" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> </com.google.android.material.tabs.TabLayout> <androidx.viewpager2.widget.ViewPager2 android:id="@+id/scheduleViewer" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginStart="0dp" android:layout_marginEnd="0dp" app:layout_constraintBottom_toTopOf="@id/menu" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/scheduleDaySelector" /> <LinearLayout android:id="@+id/menu" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="1dp" android:layout_marginEnd="1dp" android:layout_marginBottom="1dp" android:orientation="horizontal" app:layout_constraintBottom_toBottomOf="@id/scheduleViewer" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent"> <com.google.android.material.button.MaterialButton android:id="@+id/scheduleButton" style="@style/Widget.MaterialComponents.Button.TextButton.Icon" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:text="@string/schedule_button" app:icon="@drawable/ic_schedule_button" app:iconGravity="textStart" /> <com.google.android.material.button.MaterialButton android:id="@+id/announcementButton" style="@style/Widget.MaterialComponents.Button.TextButton.Icon" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:text="@string/announcement_button" app:icon="@drawable/ic_announcement_button" app:iconGravity="textStart" /> </LinearLayout> </androidx.constraintlayout.widget.ConstraintLayout>
解决方法
问题出在两处:
- ViewPager2的高度设置为
match_parent会强制占满父布局空间,忽略约束限制; - 底部按钮栏的约束错误绑定到了ViewPager2底部,导致布局依赖混乱。
修改步骤:
- 将ViewPager2的
android:layout_height="match_parent"改为android:layout_height="0dp",这样它会根据上下约束自动填充TabLayout和按钮栏之间的剩余空间; - 将底部LinearLayout的
app:layout_constraintBottom_toBottomOf="@id/scheduleViewer"改为app:layout_constraintBottom_toBottomOf="parent",让按钮栏固定在屏幕底部。
修改后的完整布局代码:
<?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" tools:context=".MainActivity"> <com.google.android.material.tabs.TabLayout android:id="@+id/scheduleDaySelector" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> </com.google.android.material.tabs.TabLayout> <androidx.viewpager2.widget.ViewPager2 android:id="@+id/scheduleViewer" android:layout_width="match_parent" android:layout_height="0dp" android:layout_marginStart="0dp" android:layout_marginEnd="0dp" app:layout_constraintBottom_toTopOf="@id/menu" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/scheduleDaySelector" /> <LinearLayout android:id="@+id/menu" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="1dp" android:layout_marginEnd="1dp" android:layout_marginBottom="1dp" android:orientation="horizontal" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent"> <com.google.android.material.button.MaterialButton android:id="@+id/scheduleButton" style="@style/Widget.MaterialComponents.Button.TextButton.Icon" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:text="@string/schedule_button" app:icon="@drawable/ic_schedule_button" app:iconGravity="textStart" /> <com.google.android.material.button.MaterialButton android:id="@+id/announcementButton" style="@style/Widget.MaterialComponents.Button.TextButton.Icon" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:text="@string/announcement_button" app:icon="@drawable/ic_announcement_button" app:iconGravity="textStart" /> </LinearLayout> </androidx.constraintlayout.widget.ConstraintLayout>
内容的提问来源于stack exchange,提问作者Егорий Зиемелис
相关产品推荐
相关产品推荐

