设为首页 加入收藏

TOP

Android Material Design控件使用(一)——ConstraintLayout 约束布局(一)
2019-09-01 23:25:36 】 浏览:69
Tags:Android Material Design 控件 使用 ConstraintLayout 约束 布局

参考文章:
约束布局ConstraintLayout看这一篇就够了
ConstraintLayout - 属性篇

介绍

Android ConstraintLayout是谷歌推出替代PrecentLayout的组件。

支持相对布局、线性布局、帧布局,看来更像是FrameLayoutLinearLayout、`RelativeLayout·三者的结合体,并且比这三者更强大的是实现了百分比布局。

大家都知道安卓碎片严重,使用百分比适配,那么将彻底解决适配问题

总结:我最近也是刚学,学完之后,发现这个布局已经将上述的所有布局的特点全部融合在一起了,使用起来简单方便的不要不要的,就是学习的属性有点多啊。

不过,多也是正常的,毕竟融合了五大布局的所有特点,学完这个布局,各种界面UI都难不倒我们了

添加依赖

implementation 'com.android.support.constraint:constraint-layout:1.1.3'

我使用的是Android Studio3.0.1版本,已经自动导入了,默认使用的就是这个布局

属性及对应的方法

我们先了解一下一些基本的概念

这里提一下,start和left其实都是指控件的左边,end和right都事指控件的右边

基本属性

注意,这里的属性都得使用命名空间来才能使用

宽高属性与之前的layout相同,wrap_contentmatch_parent但除此之外,还多出了一种,名为match contraint

实际上,这个多出来的相当于0dp,如果之前使用过LinearLayout的权重的话,应该对0dp有印象.

这里,约束布局应该是继承了Linearlayout的特性,之后我们设置权重与Linearlayout的操作也是类似,具体的请往后面看,这可是实现了百分比布局的强大布局。

属性值为控件id

  • layout_constraintLeft_toLeftOf 当前控件的左边依赖于属性控件的左边

  • layout_constraintLeft_toRightOf 当前控件的左边依赖于属性控件的右边

  • layout_constraintRight_toLeftOf

  • ayout_constraintRight_toRightOf

  • layout_constraintTop_toTopOf

  • layout_constraintTop_toBottomOf

  • layout_constraintBottom_toTopOf

  • layout_constraintBottom_toBottomOf

  • layout_constraintBaseline_toBaselineOf 当前的控件基线与属性控件的基线对齐

  • layout_constraintStart_toEndOf

  • layout_constraintStart_toStartOf

  • layout_constraintEnd_toStartOf

  • layout_constraintEnd_toEndOf

示例:

<?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">
    <Button
        android:id="@+id/btnA"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="A"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>
    <Button
        android:id="@+id/btnB"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="B"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toEndOf="@id/btnA"/>
</android.support.constraint.ConstraintLayout>

A左边依赖总布局的左边,顶部则是依赖总布局的顶部,B则是左边依赖于A的右边,顶部依赖父布局的顶部

其他的就不一一列举了,举一反三,挺容易的

mragin 边距

只有在设置了依赖,之后设置边距才会效果

这个属性不需要使用app开头,属于原本的属性

  • android:layout_marginStart 设置左边的边距

  • android:layout_marginEnd

  • android:layout_marginLeft

  • android:layout_marginTop

  • android:layout_marginRight

  • android:layout_marginBottom

例如:

<?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">

<Button
    android:id="@+
首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Android-蓝牙自动配对与隐藏对话框 下一篇读懂 Gradle 的 DSL

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目