Android ConstraintLayout 约束布局(一)

世上无难事只怕有心人

前言

在Android开发中最常见的问题,除了报空指针,那就是屏幕的适配问题,针对空指针的问题,在另一篇博客中有提到过Android的新的官方开发语言Kotin(点击查看),可以完美地解决这个问题,在这里不再过多地解释,而接下来,所介绍的针对屏幕界面适配和编写的问题,Android官方提供了一个新的布局(也不算是新的,在Android studio2.3.3就已经出现)——ConstraintLayout,使用这个布局,可以很方便地进行Android界面拖拽式开发,极大地提高了开发者的开发效率,解决了很多的布局方面的问题,在屏幕适配上也有较好的表现:

这里写图片描述

ConstraintLayout的使用

从Android studio2.3.3开始约束布局就已经成了开发工具默认使用的布局了,如果你的开发工具低于这个版本,那就需要在 build.gradle 引入支持库:

dependencies {
    compile 'com.android.support.constraint:constraint-layout:1.0.1'
}

在传统的界面编写过程中,Android开发人员基本上都是通过XML文件的编写来进行开发,这在一方面极大地限制了开发者的开发效率,给开发进度带来了很大的不必要消耗,相信大部分开发人员在工作遇到过这样的情况,同一个项目Android和IOS同时开始,一般情况下Android总会比IOS慢几天的工作进度,在这里面,界面的编写占了很大的一部分原因,使得Android的工作进度缓慢

而ConstraintLayout就是为了解决这一现状而出现的。它和传统编写界面的方式恰恰相反,ConstraintLayout非常适合使用可视化的方式来编写界面,但并不太适合使用XML的方式来进行编写。当然,可视化操作的背后仍然还是使用的XML代码来实现的,只不过这些代码是由Android Studio根据我们的操作自动生成的

另外,ConstraintLayout 还有一个优点,它可以有效地解决布局嵌套过多的问题。我们平时编写界面,复杂的布局总会伴随着多层的嵌套,而嵌套越多,程序的性能也就越差。ConstraintLayout则是使用约束的方式来指定各个控件的位置和关系的,它有点类似于 RelativeLayout,但远比RelativeLayout要更强大。

常用方法

layout_constraintTop_toTopOf       // 将所需视图的顶部与另一个视图的顶部对齐。 

layout_constraintTop_toBottomOf    // 将所需视图的顶部与另一个视图的底部对齐。 

layout_constraintBottom_toTopOf    // 将所需视图的底部与另一个视图的顶部对齐。 

layout_constraintBottom_toBottomOf // 将所需视图的底部与另一个视图的底部对齐。 

layout_constraintLeft_toTopOf      // 将所需视图的左侧与另一个视图的顶部对齐。 

layout_constraintLeft_toBottomOf   // 将所需视图的左侧与另一个视图的底部对齐。 

layout_constraintLeft_toLeftOf     // 将所需视图的左边与另一个视图的左边对齐。 

layout_constraintLeft_toRightOf    // 将所需视图的左边与另一个视图的右边对齐。 

layout_constraintRight_toTopOf     // 将所需视图的右对齐到另一个视图的顶部。

layout_constraintRight_toBottomOf  // 将所需视图的右对齐到另一个的底部。

layout_constraintRight_toLeftOf    // 将所需视图的右边与另一个视图的左边对齐。

layout_constraintRight_toRightOf   // 将所需视图的右边与另一个视图的右边对齐。

constraintDimensionRatio

这个属性就是把一个View的尺寸设为特定的宽高比,比如设置一张图片的宽高比为 1:1,4:3, 16:9 等。通过使用ConstraintLayout,只需使用layout_constraintDimensionRatio属性即可,如果你需要根据宽度的现有长度来计算高度的比例,则需要让android:layout_height设置为0dp,反之设置android:layout_width为0即可

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.administrator.pxdemo.MainActivity">

    <ImageView
        android:id="@+id/img"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@mipmap/ic_launcher"
        app:layout_constraintDimensionRatio="4:1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="10dp"
        app:layout_constraintDimensionRatio="4:2"
        android:background="@mipmap/ic_launcher"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/img" />


</android.support.constraint.ConstraintLayout>

效果图

这里写图片描述

偏移比例

当我们的布局文件是下面这样的时候:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/constraintLayout"
    tools:context="com.constraintlayout.app.MainActivity"
    >

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />

</android.support.constraint.ConstraintLayout>

我们得到的布局效果如下:
这里写图片描述

那么我们有个疑问,为什么Button 是居中显示的?因为在上面的布局中有两个重要的属性没有写出来,但是却有默认的属性值,那就是水平、垂直的偏移比例。

layout_constraintHorizontal_bias  //控件的水平偏移比例

layout_constraintVertical_bias   //控件的垂直偏移比例

如果在布局文件中没有明确的写出偏移比例,那么系统默认偏移比例值为:0.5 。
到这里我们已经清楚了,上面的布局文件就相当于:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.administrator.pxdemo.MainActivity"
    tools:layout_editor_absoluteY="81dp">

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintVertical_bias="0.5"
        />


</android.support.constraint.ConstraintLayout>

上面的布局就相当于

这里写图片描述

如果设置偏移量分别为0.3和0.9,那就是下面的效果

这里写图片描述

基线约束控键

该控键帮助你对齐任意两个widget的文字部分,与widget的大小无关。例如你有两个不同尺寸的widget但是你想要他们的文字部分对齐。

layout_constraintBaseline_toBaselineOf

效果图:
这里写图片描述
这里写图片描述

3.0之后可能不会再界面上显示操作的方法,需要在代码中编写一下或者在属性中进行设置

这里写图片描述

今天的博客暂时就写到这里,以后的时间还长,以后慢慢的在博客中学习这个博客之后的用法,世上无难事只怕有心人,付出总会有收获

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android Studio约束布局是一种强大而灵活的布局方式,它允许您以声明性的方式定义视图之间的关系。以下是使用约束布局的一些基本步骤: 1. 在布局文件中使用`ConstraintLayout`作为根视图。在XML文件中,您可以这样声明一个约束布局: ``` <androidx.constraintlayout.widget.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"> <!-- 在这里添加其他视图 --> </androidx.constraintlayout.widget.ConstraintLayout> ``` 2. 定义视图之间的约束关系。您可以通过拖动和放置视图来在设计编辑器中设置约束,也可以在XML文件中手动编写约束。例如,要将一个按钮位于父布局顶部,并与左右边缘有10dp的间距,您可以这样定义约束: ``` <Button android:id="@+id/myButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="My Button" app:layout_constraintTop_toTopOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_marginStart="10dp" app:layout_marginEnd="10dp"/> ``` 3. 可选地,您还可以使用链(chains)和辅助对象(guidelines)来进一步定义视图之间的关系,以及在不同屏幕尺寸下的自适应布局约束布局的优势在于它可以适应各种屏幕尺寸和方向,并且可以减少嵌套布局的需要。您可以通过在Android Studio的设计编辑器中直观地操作视图和约束,或者手动编辑XML文件来创建约束布局。要了解更多关于约束布局的信息和用法,请参阅Android官方文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值