android Studio2.2 新布局 ConstranintLayout入门1

android Studio2.2 新布局 ConstranintLayout入门

今天发现AS更新2.2正式版本,同时给我们带来了一个新的布局ConstraintLayout,让我们来一起研究学习一下.
简单的来说,约束布局就是相对布局,强调控件之间的联系,同时能让你的布局减少层级嵌套

需要的相关:

android studio 2.2版本 打开AS就可以更新

在项目的build里添加依赖:

dependencies {
    compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha8'
}
在xml里定义:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/cons"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.zhangqi.constranintlayout.MainActivity">

    <ImageView
        android:id="@+id/image"
        android:src="@mipmap/ic_launcher"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</android.support.constraint.ConstraintLayout>
来看一下2.2给我们带来的PreView界面

image

现在我们把图片置于布局的中间我们该怎么做呢?
在相对布局中我们可以使用下面这个属性使控件位于父布局中间
 android:layout_centerInParent="true"
而在约束布局中,我们则要对控件进行约束
<ImageView
        android:id="@+id/image"
        android:src="@mipmap/ic_launcher"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.49"
        />
我们看到了添加了四个属性,单独拿出其中一个
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraint[我]_[目标]="[目标ID]"
通俗的讲就是,[我] (imageView)的底部与目标ID的底部对[目标ID]控件的底部对齐,这样,我们就好理解了,其他属性也大致相同,这里可以写成parent既是与父布局约束,也可以使用id
app:layout_constraintBottom_toBottomOf="@id/cons"

刚才在布局中,我们看到了类似蓝图一样的布局,我们发现控件的四周出现了控制器:

image image

我们看到imageView正四周有圆形控制器,用来创建控件之间的约束,我们现在就来动手创建约束

image

通过手动方式,我们给imageView约束在父布局中,但是也看到了有默认的margin值.
现在,既然可以添加约束,当然也可以清除约束,我们看一下在蓝图中,如何清除控件的约束.

image

最后我们来实现一个简单的效果,讲图片居中,并让蚊子置于图片右边平行

image

来看一下如何去操作,添加控件之间的约束

image

当然,这篇博客只是简单的玩了一下ConstranintLayout.

如果你运行程序出现了以下错误

Error:Execution failed for task ':app:transformResourcesWithMergeJavaResForDebug'.
> com.android.build.api.transform.TransformException: com.android.builder.packaging.DuplicateFileException: Duplicate files copied in APK META-INF/LICENSE
    File1: C:\Users\zhangqi\.gradle\caches\modules-2\files-2.1\com.fasterxml.jackson.core\jackson-databind\2.7.2\84ffa765dd258dbab8695963c41308b054f3a1cb\jackson-databind-2.7.2.jar
    File2: C:\Users\zhangqi\.gradle\caches\modules-2\files-2.1\com.fasterxml.jackson.core\jackson-core\2.7.2\8b8310381b690e317f5f0574e9b2dd7034778b4c\jackson-core-2.7.2.jar
    File3: C:\Users\zhangqi\.gradle\caches\modules-2\files-2.1\com.fasterxml.jackson.core\jackson-annotations\2.7.0\19f42c154ffc689f40a77613bc32caeb17d744e3\jackson-annotations-2.7.0.jar

请在app的build.gradle添加一下代码

packagingOptions {
        exclude 'META-INF/NOTICE' // will not include NOTICE file
        exclude 'META-INF/LICENSE' // will not include LICENSE file
    }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值