ConstraintLayout约束布局的概念与使用

2023-11-20

ConstraintLayout(约束布局), 是2016年Google I/O最新推出的Android布局, 目前还在完善阶段. 从推出的力度而言, 应该会成为主流布局样式. 在最新版本的Android Studio中, ConstraintLayout已经成为默认布局.

ConstraintLayout约束布局的概念与使用 - 简书

使用ConstraintLayout布局的最新版本1.0.0-alpha4, 需要下载最新Canary版本的Android Studio, 下载地址.

ConstraintLayout作为非绑定(Unbundled)的支持库, 命名空间是app:, 来自于本地包的命名空间. 本文介绍ConstraintLayout布局的常见使用方式.

本文源码的GitHub下载地址

概念

ConstraintLayout约束布局的含义: 根据布局中的其他元素或视图, 确定View在屏幕中的位置. 包含三个重要信息, 根据其他视图设置位置, 根据父容器设置位置, 根据基准线设置位置.

layout_constraint[本源]_[目标]="[目标ID]"

例如:

app:layout_constraintBottom_toBottomOf="@+id/constraintLayout"

约束当前View的底部目标View的底部, 目标View是constraintLayout. 表明, 把当前View放置到constraintLayout(父容器)的底部, 并且底部一致.

为了演示多个示例, 使用复用的Activity页面. 根据参数设置标题和布局Id.

public class LayoutDisplayActivity extends AppCompatActivity {
    private static final String TAG = LayoutDisplayActivity.class.getSimpleName();
    static final String EXTRA_LAYOUT_ID = TAG + ".layoutId"; // 布局ID

    @Override protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle(getIntent().getStringExtra(Intent.EXTRA_TITLE));
        final int layoutId = getIntent().getIntExtra(EXTRA_LAYOUT_ID, 0);
        setContentView(layoutId); // 设置页面布局, 复用布局
    }
}

主页面使用ListView展示多个项, 每项都是不同的布局. 点击项发送不同的Intent, 填充所要显示的页面.

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ListView list = (ListView) findViewById(R.id.activity_main);
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
                android.R.layout.simple_list_item_1, LIST_ITEMS);
        list.setAdapter(adapter);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                // 复用显示布局
                Intent intent = new Intent(MainActivity.this, LayoutDisplayActivity.class);
                intent.putExtra(Intent.EXTRA_TITLE, LIST_ITEMS[i]); // 标题
                intent.putExtra(LayoutDisplayActivity.EXTRA_LAYOUT_ID, LAYOUT_IDS[i]); // 布局Id
                startActivity(intent);
            }
        });
    }
}

基础

ConstraintLayout布局最基本的使用方式, 就是直接指定位置. 取消按钮的底部对齐constraintLayout(父容器)的底部, 左侧对齐父容器的左侧. 下一步按钮的底部对齐父容器的底部, 而左侧对齐取消按钮的右侧. 并且每个按钮边缘有Margin空隙.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    android:id="@+id/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">

    <Button
        android:id="@+id/cancel_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:layout_marginStart="16dp"
        android:text="取消"
        app:layout_constraintBottom_toBottomOf="@id/constraintLayout"
        app:layout_constraintStart_toStartOf="@id/constraintLayout"/>

    <Button
        android:id="@+id/next_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:layout_marginStart="16dp"
        android:text="下一步"
        app:layout_constraintBottom_toBottomOf="@id/constraintLayout"
        app:layout_constraintStart_toEndOf="@id/cancel_button"/>

</android.support.constraint.ConstraintLayout>

ConstraintLayout的属性设置, 最重要的就是理解属性的表示含义.

ConstraintLayout约束布局的概念与使用 - 简书

 

比例

ConstraintLayout布局除了对齐属性, 还有重要的比例属性. 中心(Center)按钮需要把全部边界与constraintLayout(父容器)边界对齐, 则为居中. 同时还可以设置水平与竖直的比例, 如0.25. Bias按钮设置水平与竖直的比例是0.25, 表示左侧与右侧比例是1:4, 上部与下部的比例是1:4.

constraintHorizontal_bias设置水平比例, constraintVertical_bias设置竖直比例.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/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">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Center"
        app:layout_constraintEnd_toEndOf="@id/constraintLayout"
        app:layout_constraintStart_toStartOf="@id/constraintLayout"
        app:layout_constraintTop_toTopOf="@+id/constraintLayout"
        app:layout_constraintBottom_toBottomOf="@+id/constraintLayout"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Bias"
        app:layout_constraintEnd_toEndOf="@id/constraintLayout"
        app:layout_constraintStart_toStartOf="@id/constraintLayout"
        app:layout_constraintTop_toTopOf="@+id/constraintLayout"
        app:layout_constraintBottom_toBottomOf="@+id/constraintLayout"
        app:layout_constraintHorizontal_bias="0.25"
        app:layout_constraintVertical_bias="0.25"/>

</android.support.constraint.ConstraintLayout>

tools:layout_editor_absoluteX属性对于视图起到辅助作用, 理解边界的真实距离, 点击可视化布局会自动生成.

ConstraintLayout约束布局的概念与使用 - 简书

 

引导线

ConstraintLayout布局除了与布局对齐以外, 还可以与引导线(Guideline)对齐. 设置竖直引导线(Guideline)距离左侧72dp. 按钮(Button)的左侧都与引导线对齐, 上下使用比例的方式排列, 一个0.25比例, 一个0.75比例.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/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.support.constraint.Guideline
        android:id="@+id/guideLine"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_begin="72dp"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Guide Up"
        app:layout_constraintStart_toStartOf="@id/guideLine"
        app:layout_constraintTop_toTopOf="@+id/constraintLayout"
        app:layout_constraintBottom_toBottomOf="@+id/constraintLayout"
        app:layout_constraintVertical_bias="0.25"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Guide Down"
        app:layout_constraintStart_toStartOf="@id/guideLine"
        app:layout_constraintTop_toTopOf="@+id/constraintLayout"
        app:layout_constraintBottom_toBottomOf="@+id/constraintLayout"
        app:layout_constraintVertical_bias="0.75"/>

</android.support.constraint.ConstraintLayout>

ConstraintLayout约束布局的概念与使用 - 简书

 

视图尺寸

ConstraintLayout布局中的控件也可以设置填充尺寸. 控件把宽度设置为0dp会自动根据位置进行填充. 如Large按钮, 左侧对齐与Small按钮的左侧, 右侧对齐与constraintLayout父控件的右侧, 宽度设置为0dp, 实际会填充全部位置.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    android:id="@+id/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">

    <Button
        android:id="@+id/small"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Small"
        app:layout_constraintStart_toStartOf="@id/constraintLayout"
        app:layout_constraintTop_toTopOf="@id/constraintLayout"/>

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Large"
        app:layout_constraintBottom_toBottomOf="@id/constraintLayout"
        app:layout_constraintEnd_toEndOf="@id/constraintLayout"
        app:layout_constraintStart_toEndOf="@id/small"
        app:layout_constraintTop_toTopOf="@id/constraintLayout"/>

</android.support.constraint.ConstraintLayout>

ConstraintLayout约束布局的概念与使用 - 简书

 

视图纵横比

ConstraintLayout布局还可以使用constraintDimensionRatio设置视图的纵横比, 则需要把宽(layout_width)或者高(layout_height)设置为0dp, 根据另一个属性和比例, 计算当前属性, 如两个图片控件的显示大小.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    android:id="@+id/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">

    <ImageView
        android:layout_width="0dp"
        android:layout_height="200dp"
        android:background="@color/colorAccent"
        android:src="@drawable/total_large"
        app:layout_constraintBottom_toBottomOf="@+id/constraintLayout"
        app:layout_constraintLeft_toLeftOf="@+id/constraintLayout"
        app:layout_constraintRight_toRightOf="@+id/constraintLayout"
        app:layout_constraintTop_toTopOf="@+id/constraintLayout"
        app:layout_constraintVertical_bias="0.0"/>

    <ImageView
        android:layout_width="0dp"
        android:layout_height="200dp"
        android:background="@color/colorAccent"
        android:contentDescription="@null"
        android:src="@drawable/total_large"
        app:layout_constraintBottom_toBottomOf="@+id/constraintLayout"
        app:layout_constraintDimensionRatio="4:3"
        app:layout_constraintLeft_toLeftOf="@+id/constraintLayout"
        app:layout_constraintRight_toRightOf="@+id/constraintLayout"/>

</android.support.constraint.ConstraintLayout>

 

ConstraintLayout约束布局的基本使用方式就是这些, 可以观察到ConstraintLayout布局兼顾LinearLayout与RelativeLayout的优点, 非常适合构建复杂布局, 会成为Android的主流布局方式.

OK, that's all! Enjoy it!

来自:http://www.jianshu.com/p/32a0a6e0a98a

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ConstraintLayout约束布局的概念与使用 的相关文章

  • ul里面可以放div吗?

    在HTML中 ul 标签代表无序列表 可以用来展示项目列表 而 div 标签则是div容器用于分组内容 提供独立于文档的CSS样式和JavaScript事件处理 那么 ul里面可以放div吗 答案是肯定的 下面从多个方面进行详细阐述 一 语
  • MacBook安装使用XMind

    MacBook安装使用XMind XMind简介 官方地址 https www xmind cn XMind 是一个全功能的思维导图和头脑风暴软件 为激发灵感和创意而生 作为一款有效提升工作和生活效率的生产力工具 受到全球百千万用户的青睐
  • 计算机内存插在主板的哪个槽,四个内存插槽,这是正确的安装顺序

    具有防呆设计的主板插槽几乎不可能错误地插入 因此很有可能在第一个插槽中安装了内存 尽管可以 但是会阻止内存在最佳状态下工作 主板手册中有这样一句话 说明 为便于理解 首先对内存插槽编号 从靠近CPU插槽的位置开始 主板 针对不同情况的最佳安
  • 区域生长算法及其实现

    区域生长算法及其实现 背景 前面我们已经介绍了 最大熵分割法 OTSU算法 他们都有各自的优缺点 通常都不是单独使用这些算法 需要和其它算法来结合使用 前面两类算法都是单独对图像的灰度信息进行处理 不包含图像的空间信息 而区域生长算法则包含
  • 云计算与大数据概论第十一周

    分布式计算 分布式计算是一种计算方法 和集中式计算是相对的 随着计算技术的发展 有些应用需要非常巨大的计算能力才能完成 如果采用集中式计算 需要耗费相当长的时间来完成 分布式计算将该应用分解成许多小的部分 分配给多台计算机进行处理 这样可以
  • 如何快速开发一个简单实用的MES系统?

    01 如何快速开发一个简单实用的MES系统 MES生产管理系统 又称制造执行系统 是一种集成了计划 生产 质量控制 库存管理和材料申请等生产流程的管理系统 是企业中实现高效生产的重要一环 根据题主描述 通过产品条形码实现对生产计划下的产品追
  • 小程序的基础(三)

    文章目录 前言 一 navigator 二 rich text 作用 1 nodes属性 注意 三 button open type 的 contact的实现流程 代码示例 1 contact 直接打开 客服对话功能 需要在微信小程序的后台
  • int argc,char*argv[ ]的简洁解释

    1 arguments argument counter 计数个数 和 argument vector 矢量 带有方向的变量参数 也就是指针 argc命令行输入参数的个数 int main int argc char argv int i
  • 【操作系统】王道考研 p42 段页式管理方式

    段页式管理方式 知识总览 分段 分页管理方式中最大的优缺点 关于段式管理会产生外部碎片 ps 分段管理中产生的外部碎片也可以用 紧凑 来解决 只是需要付出较大的时间代价 分段 分页 段页式管理 示意图 先分段 后分页 段页式管理的逻辑地址结
  • oracle 的路径不一致,DG环境搭建,在备库遇到问题,主备库的路径不一致

    现在在做oracle DG的环境搭建 因为实际生产的原因 主备库的路径是不一致的 我把主库的rman文件传到备库后 在备库进行恢复 无法恢复 求指导 oracle std bak rman target sys 123456 pri aux
  • java变量作用域和堆栈

    一 作用域决定了变量的可见性和生命周期 java中变量分为成员变量和局部变量 如下 1 成员变量 在类的所有方法外部声明的变量 即类所拥有的变量 可以被系统初始化 1 1静态成员变量 类被加载时被创建 其生命周期与该类的生命周期相同 1 2
  • 在自己的bash脚本中实现自动补全

    在90年代Linux和DOS共存的年代里 Linux的Shell们有一个最微不足道但也最实用的小功能 就是命令自动补全 而DOS那个笨蛋一直到死都没学会什么叫易用 Linux的这个微不足道的小传统一直延续至今 虽然看似微不足道 其实也极大的
  • stm32无法唤醒DTH11温湿度传感器解决

    关于DTH11的介绍和使用方法可以随便搜索一下别的文章 直接搜索DTH11即可 这里使用艾克姆科技的例程 却无法成功运行 上了示波器才发现拉低时间无法达到18ms 因此无法唤醒DTH11 总线由stm32拉低12ms左右之后就一直处于高电平
  • CString字符串查找和截取

    1 Find 该函数从左侧0索引开始 查找第一个出现的字符位置 CString str abc int postion str Find a 如果查到 返回以0索引起始的位置 未查到 返回 1 如果查到 返回以0索引起始的位置 未查到 返回
  • 卷积神经网络CNN小结(简单实现代码mnist数据集)

    由于全连接神经网络处理图像中的需要训练参数过多的问题 而卷积神经网络中 卷积层的神经元只与前一层的部分 神经元节点相连 既它的神经元的连接是非全连接的 且同一层某些神经元之间的连接的权重w和偏移b是共享的 这样大量减少了训练参数的数量 图1
  • 多线程事务怎么回滚?说用 @Transactional 可以回去等通知了!

    背景介绍 1 最近有一个大数据量插入的操作入库的业务场景 需要先做一些其他修改操作 然后在执行插入操作 由于插入数据可能会很多 用到多线程去拆分数据并行处理来提高响应时间 如果有一个线程执行失败 则全部回滚 2 在spring中可以使用 T
  • C++day4(关系运算符的重载)

    关系运算符重载的作用 可以让两个自定义类型对象进行对比操作 代码实现关系运算符的重载 include
  • unity工程崩溃资源找回

    1 Unity死机未保存场景 当你在Unity中编辑场景 突然死机时 可以在项目文件目录中找到Temp文件夹 双击文件夹 找到 Backupscenes文件夹 把后缀为 backup的文件后缀改为 unity 然后拖进Unity的Proje
  • 2021-05-03

    一 R中安装 phyloseq 的方法 很多网上的教程使用的都是 source https bioconductor org biocLite R biocLite phyloseq 但是我尝试了很多次 最后还是没有成功 下面能成功安装 p

随机推荐