logo头像
Snippet 博客主题

ConstrantLayout布局相关

本文于 903 天之前发表,文中内容可能已经过时。

定义

一个功能更加强大的RelativeLayout,谷歌官网推荐

功能

  1. 约束

MotionLayout

  1. 是ConstraintLayout的子类
  2. 在单独的xml文件中设置约束条件
  3. 直接在两个ConstrainSet之间进行动画过度
    • 过度效果一样
    • 动画定制

作用

  1. 可折叠的header
  2. 状态反馈
  3. 过度效果
  4. 幻灯片?

1.布局高度使用match_parent时,填充整个屏幕

若采用以下布局

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
android:id="@+id/btn_download"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下载"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_file_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@id/btn_download" />

</androidx.constraintlayout.widget.ConstraintLayout>

recycleView会占据整个屏幕,而不会受到Button的约束,但是一旦recycleView的高度使用android:layout_height="wrap_content"则不会出现问题,经过查阅官方文件。

Important: MATCH_PARENT is not supported for widgets contained in a ConstraintLayout, though similar behavior can be defined by using MATCH_CONSTRAINT with the corresponding left/right or top/bottom constraints being set to “parent”.

  1. ConstraintLayout不支持match_parent属性,要使用match_constraint代替,即将设置为0dp就可以实现recycleViewButton下的约束了