约束布局ConstraintLayout嵌入RecycleVie
2019-09-25 本文已影响0人
为自己代颜_
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:minHeight="?attr/actionBarSize"
app:layout_constraintTop_toTopOf="parent"
app:tabBackground="@color/colorPrimary"
app:tabIndicatorColor="@android:color/white"
app:tabMode="fixed"
app:tabSelectedTextColor="@android:color/white"
app:tabTextColor="@android:color/white">
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="@id/tab_layout"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_weight="1">
</android.support.v4.view.ViewPager>
</android.support.constraint.ConstraintLayout>
ViewPager中的Fragment,放的就是图中的那个RecyclerView(RecyclerView的最后一个条目显示不全)。ViewPager的高度设为0dp,app:layout_constraintVertical_weight为1,其实就和LinearLayout中的weight是一样的。 app:layout_constraintTop_toBottomOf="@id/tab_layout"表示ViewPager在TabLayout的下面。
RecyclerView最后一个条目为什么会显示不全?这是因为在ConstraintLayout中,如果height设置为0,就一定要给控件设置上约束和下约束。这里的ViewPager只设置了上约束在tablayout的下面,而没有设置下约束。只要加上app:layout_constraintBottom_toBottomOf=“parent”,给ViewPager设置一个下约束在parent的上面,问题就解决了
关键点:android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="@id/tab_layout"
app:layout_constraintBottom_toBottomOf="parent"