最近在项目中使用了一下ConstrantLayout,发觉这个布局还是十分好用的!
一、xml设置
简单的说,可以实现以下定位:相对定位,角度定位,居中定位,链。在应对相对复杂的布局时就可以直接摒弃RelativeLayout和LinearLayout,直接使用ConstrainLayout即可。
我使用中感觉很好用的功能有:
1、居中定位,并可通过权重或者margin调节偏移量。
2、角度定位,可以很方便实现两个控件间的相对关系
3、layout_constraintBaseline_toBaselineOf可以实现文本基准线对齐。
总之,一个ContraintLayout综合了RelativeLayout和LinearLayout以及比例布局,可以很有效的降低布局的层级,是一个好东西!
关于ConstraintLayout参考下面这篇博文就行,写的很详细,感谢博主!
约束布局ConstraintLayout看这一篇就够了
二、代码设置
上面的文章是介绍xml中进行布局使用的,如果想要使用代码进行设置呢?根据官方文档,就要用到ConstraintSet这个类了。下面的文章也基本介绍了如何使用代码进行设置。
ConstraintLayout 在代码中动态设置约束。
如果使用过程中报如下错误:
All children of ConstraintLayout must have ids to use ConstraintSet
需要检查ContraintLayout的子布局是不是都设置了id,如果没有加上即可。
参考:https://blog.csdn.net/qq_27454233/article/details/84143980
举例如下:
布局为:R.layout.ease_fragment_contact_list。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/cl_root"
xmlns:app="http://schemas.android.com/apk/res-auto">
<ViewStub
android:id="@+id/view_stub"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/srl_contact_refresh"/>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/srl_contact_refresh"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/view_stub"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginTop="8dp">
<com.xxx.xxx.widget.EaseRecyclerView
android:id="@+id/rv_contact_list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
<com.xxx.xxx.widget.EaseSidebar
android:id="@+id/side_bar_friend"
android:layout_width="30dp"
android:layout_height="0dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/view_stub"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
app:ease_side_bar_text_size="16sp"
app:ease_side_bar_head_arrays="@array/side_bar_array"/>
<TextView
android:id="@+id/floating_header"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_centerInParent="true"
android:background="@drawable/ease_show_head_toast_bg"
android:gravity="center"
android:paddingLeft="25dp"
android:paddingRight="25dp"
android:textColor="@android:color/white"
android:textSize="40sp"
android:visibility="gone"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
想要将view_stub初始化为搜索布局,如下:
布局为:R.layout.demo_layout_search。
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rl_search_root"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.xxx.xxx.widget.EaseSearchTextView
android:id="@+id/tv_search"
android:layout_width="match_parent"
android:layout_height="36dp"
android:hint="@string/em_conversation_list_search_hint"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_marginRight="16dp"
android:layout_marginEnd="16dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="3dp"/>
</FrameLayout>
代码如下:
//添加搜索会话布局
viewStub.setLayoutResource(R.layout.demo_layout_search);
View view = viewStub.inflate();
ViewGroup.LayoutParams params = view.getLayoutParams();
if(params instanceof ConstraintLayout.LayoutParams) {
ConstraintSet set = new ConstraintSet();
//R.layout.ease_fragment_contact_list为ConstraintLayout为最外的布局
set.clone(mContext, R.layout.ease_fragment_contact_list);
//或者采用如下也可,此处的mConstraintLayout指要设置约束关系的控件的父类
//set.clone(mConstraintLayout);
//设置view与父布局左对齐,对应xml中的app:layout_constraintLeft_toLeftOf="parent"
set.connect(view.getId(), ConstraintSet.LEFT, ConstraintSet.PARENT_ID, ConstraintSet.LEFT);
//设置view与父布局右对齐,对应xml中的app:layout_constraintRight_toRightOf="parent"
set.connect(view.getId(), ConstraintSet.RIGHT, ConstraintSet.PARENT_ID, ConstraintSet.RIGHT);
//设置view与父布局顶部对齐,对应xml中的app:layout_constraintTop_toTopOf="parent"
set.connect(view.getId(), ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP);
//设置view的底部与srl_contact_refresh的顶部对齐,对应xml中的app:layout_constraintBottom_toTopOf="@id/srl_contact_refresh"
set.connect(view.getId(), ConstraintSet.BOTTOM, R.id.srl_contact_refresh, ConstraintSet.TOP);
//设置view的width
set.constrainWidth(view.getId(), ViewGroup.LayoutParams.MATCH_PARENT);
//设置view的height
set.constrainHeight(view.getId(), ViewGroup.LayoutParams.WRAP_CONTENT);
//设置srl_contact_refresh的顶部与view的底部对齐,对应xml中的app:layout_constraintTop_toBottomOf="@id/view"
set.connect(R.id.srl_contact_refresh, ConstraintSet.TOP, view.getId(), ConstraintSet.BOTTOM);
//设置side_bar_friend的顶部与view的底部对齐,对应xml中的app:layout_constraintTop_toBottomOf="@id/view"
set.connect(R.id.side_bar_friend, ConstraintSet.TOP, view.getId(), ConstraintSet.BOTTOM);
ConstraintLayout clRoot = findViewById(R.id.cl_root);
//最后将约束关系是指给ContraintLayout
set.applyTo(clRoot);
}
tvSearch = view.findViewById(R.id.tv_search);
tvSearch.setHint(R.string.em_friend_list_search_hint);