原文链接:https://juejin.im/post/5a31ee3af265da43294e0fe1
添加依赖
compile 'com.android.support.constraint:constraint-layout:1.0.2'
创建布局
-
<?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:orientation="vertical"> -
<Button -
android:id="@+id/bt_a" -
android:layout_width="0dp" -
android:layout_height="wrap_content" -
android:text="scroller_scroll" -
android:textSize="12dp" -
app:layout_constraintBottom_toTopOf="@+id/button3" -
app:layout_constraintHorizontal_weight="1" -
app:layout_constraintLeft_toLeftOf="parent" -
app:layout_constraintRight_toLeftOf="@+id/bt_b"/> -
<Button -
android:id="@+id/bt_b" -
android:layout_width="0dp" -
android:layout_height="wrap_content" -
android:text="scroller_fling" -
android:textSize="12dp" -
app:layout_constraintBottom_toTopOf="@+id/button3" -
app:layout_constraintHorizontal_weight="1" -
app:layout_constraintLeft_toRightOf="@+id/bt_a" -
app:layout_constraintRight_toLeftOf="@+id/bt_c"/> -
<Button -
android:id="@+id/bt_c" -
android:layout_width="0dp" -
android:layout_height="wrap_content" -
android:text="over_scroll" -
android:textSize="12dp" -
app:layout_constraintBottom_toTopOf="@+id/button3" -
app:layout_constraintHorizontal_weight="1" -
app:layout_constraintLeft_toRightOf="@+id/bt_b" -
app:layout_constraintRight_toRightOf="@+id/bt_d"/> -
<Button -
android:id="@+id/bt_d" -
android:layout_width="0dp" -
android:layout_height="wrap_content" -
android:text="overs_fling" -
android:textSize="12dp" -
app:layout_constraintBottom_toTopOf="@+id/button3" -
app:layout_constraintHorizontal_weight="1" -
app:layout_constraintLeft_toRightOf="@+id/bt_c" -
app:layout_constraintRight_toRightOf="parent"/> -
<Button -
android:id="@+id/button3" -
android:layout_width="0dp" -
android:layout_height="wrap_content" -
android:layout_marginEnd="8dp" -
android:layout_marginStart="8dp" -
android:layout_marginTop="8dp" -
android:text="Button" -
app:layout_constraintBottom_toBottomOf="parent" -
app:layout_constraintEnd_toEndOf="parent" -
app:layout_constraintStart_toStartOf="parent" -
/> -
</android.support.constraint.ConstraintLayout> -
复制代码
需要了解的地方
- 宽度为0dp
android:layout_width="0dp" - 需要准确指定左右的约束
app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toLeftOf="@+id/bt_b"//使用@id 编译不通过,使用@+id就可以了 - 指定权重
app:layout_constraintHorizontal_weight="1" - 约束也可以为
parentapp:layout_constraintBottom_toBottomOf="parent"
转载于:https://juejin.im/post/5a31ee3af265da43294e0fe1
本文详细介绍了如何在Android中使用ConstraintLayout进行布局设计,包括添加依赖、创建布局、设置按钮宽度为0dp、指定左右约束、使用权重等技巧,以及如何正确使用@+id。
3484

被折叠的 条评论
为什么被折叠?



