ConstraintLayout
suggest changeVersions
ConstraintLayout ships in the support library (com.android.support.constraint:constraint-layout), not as a framework class from Android 2.3. The library itself runs on API 9 and higher.
Introduction
ConstraintLayout is a ViewGroup which allows you to position and size widgets in a flexible way. It is compatible with Android 2.3 (API level 9) and higher.
It can express large layouts with a flat view hierarchy. Like RelativeLayout, it places views by relationships to siblings and to the parent. It is more flexible than RelativeLayout, and Android Studio's Layout Editor edits it directly.
Syntax
- ConstraintLayout
- public void addView(View child, int index, ViewGroup.LayoutParams params)
- public ConstraintLayout.LayoutParams generateLayoutParams(AttributeSet attrs)
- public void onViewAdded(View view)
- public void onViewRemoved(View view)
- public void removeView(View view)
- public void requestLayout()
- protected boolean checkLayoutParams(ViewGroup.LayoutParams params)
- protected ConstraintLayout.LayoutParams generateDefaultLayoutParams()
- protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams params)
- protected void onLayout(boolean changed, int left, int top, int right, int bottom)
- protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
- ConstraintLayout.LayoutParams
- public void resolveLayoutDirection(int layoutDirection)
- public void validate()
- protected void setBaseAttributes(TypedArray a, int widthAttr, int heightAttr)
Parameters
| Parameter | Details |
|---|---|
| child | The View to add to the layout. |
| index | The index of the View in the layout hierarchy. |
| params | The LayoutParams of the View. |
| attrs | The AttributeSet that defines the LayoutParams. |
| view | The View that was added or removed. |
| changed | Whether this View changed size or position. |
| left | The left position, relative to the parent View. |
| top | The top position, relative to the parent View. |
| right | The right position, relative to the parent View. |
| bottom | The bottom position, relative to the parent View. |
| widthMeasureSpec | The horizontal space requirements from the parent. |
| heightMeasureSpec | The vertical space requirements from the parent. |
| layoutDirection | The resolved layout direction passed to resolveLayoutDirection. |
| a | Styled attributes read by setBaseAttributes. |
| widthAttr | Index of the layout_width attribute. |
| heightAttr | Index of the layout_height attribute. |
Remarks
Google announced ConstraintLayout at Google I/O 2016, when the library was still a beta. The 1.0 support artifact used elsewhere in this book is a stable release.
For more about ConstraintLayout:
https://codelabs.developers.google.com/codelabs/constraint-layout/index.html