关于如何在 ConstraintLayout 中将 ViewPager 始终置于 ll_title标题栏 的下方。。
如何将 ViewPager 始终置于 ll_title 下方
在 ConstraintLayout 中,可以通过约束来实现 ViewPager 始终位于 ll_title 标题栏的下方。以下是修改后的布局代码:
<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white">
<!-- 标题栏 -->
<RelativeLayout
android:id="@+id/ll_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent">
<LinearLayout
android:id="@+id/ll_title_part"
android:layout_width="match_parent"
android:layout_height="48dp"
android:padding="5dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/commonTitle_iv_back"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:layout_marginTop="-10dp"
android:padding="10dp"
android:src="@drawable/smssdk_ic_popup_dialog_close" />
</LinearLayout>
</RelativeLayout>
<!-- ViewPager -->
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="@id/ll_title"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
</android.support.constraint.ConstraintLayout>
关键点说明
-
ll_title的约束:-
app:layout_constraintTop_toTopOf="parent":将ll_title置于顶部。 -
app:layout_constraintLeft_toLeftOf="parent"和app:layout_constraintRight_toRightOf="parent":让ll_title横向充满父布局。
-
-
ViewPager的约束:-
app:layout_constraintTop_toBottomOf="@id/ll_title":将ViewPager的顶部约束到ll_title的底部。 -
app:layout_constraintBottom_toBottomOf="parent":将ViewPager的底部约束到父布局的底部。 -
app:layout_constraintLeft_toLeftOf="parent"和app:layout_constraintRight_toRightOf="parent":让ViewPager横向充满父布局。 -
android:layout_height="0dp":通过约束来分配高度,确保ViewPager填充剩余空间。
-
注意事项
-
确保
ll_title的高度不会过大,否则可能会挤压ViewPager的显示空间。 -
如果
ViewPager的内容较多,可以考虑设置滚动条或分页指示器,以提升用户体验。
通过上述布局代码,ViewPager 将始终位于 ll_title 的下方,并且会自动填充剩余空间。



















