android 布局之ConstraintLayout

其实ConstraintLayout是Android Studio 2.2中主要的新增功能之一,也是Google在2016年的I/O大会上重点宣传的一个功能。是为了android可视化编辑而做的努力。android studio 的可视化编辑个人不推荐使用,不过ConstraintLayout布局的使用还是有必要了解的。
1,要想使用ConstraintLayout需要在app的build.gradle里面引入:

  compile 'com.android.support.constraint:constraint-layout:1.0.2'

2,首先看一个简单的xml和图片效果:








 
image.png

各位看客官不难看出,xml中四个简单的Textview分布位置,以id为tv的第一个控件为基准,第二个控件在第一个控件的下方,且都在整个布局的左边;第三个控件在整个父布局的左下方;第四个控件在第一个控件的右边。
仔细观察,每个Textview都有类似的属性:
比如第一个控件的:
app:layout_constraintTop_toTopOf="parent"
第二个控件的
app:layout_constraintTop_toBottomOf="@+id/tv1"
字面意思就是:
该控件的某个边和某个控件的某个边对齐。
比如,第一个控件是该控件的上部和父布局的上部对齐,自然就使得第一个控件处于左上方;第二个控件的顶部和第一个控件即id=tv1的控件的底部对齐,自然第二个 控件就会位于第一个控件的正下方。其他举一反三即可。

现在我们规定一个布局再次试验一下,搞个最常见的布局。

image.png

代码如下:





    app:layout_constraintTop_toTopOf="parent"
 />

    app:layout_constraintRight_toLeftOf="parent"
    android:layout_marginRight="10dp"
    />

    app:layout_constraintTop_toBottomOf="@+id/tv1"
    />

    app:layout_constraintRight_toLeftOf="parent"
 
    app:layout_constraintTop_toBottomOf="@+id/tv1"
    android:layout_marginRight="10dp"
    />

image.png

未完待续...还有其他常用属性。

你可能感兴趣的:(android 布局之ConstraintLayout)