爱不释手的ConstraintLayout布局

好久没有更新博客了,主要是最近太忙了。其实ConstraintLayout这个布局早都有了,一直没有使用过,所以看到有很多网站都有介绍。但是,感觉很多都是写的很乱或者写的很模糊让人看的好像使用起来很麻烦的样子。所以自己想写一篇博客介绍一下它的使用方法,其实真的好用又简单。

  • 使用之前你的Android studio 的版本必须是2.3以上的才可以使用这个布局控件,否则你发现你无论怎么在build.gradle文件中添加官方依赖
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    都是没有什么用的。so,如果你想体验这个控件那么就先升级一下你的Android studio吧。(现在的最高版本好像是3.0的)
  • 好了,就像我刚才说的如果你的AS是2.3版本以上的话,你新创建一个项目的时候AS就会自动帮你引入这个依赖。并且如果你是从项目中直接创建一个Activity的话,默认的根部局就是这个ConstraintLayout。废话不多说,接下来就具体说一下怎么使用:
    • 你跟这个我看这个布局就可以很快的入门这个布局的时候。感觉这个布局最重要的一点就是你一定要懂得约束力这个词。“约束力”是这篇博客也是这个布局最重要的点。
    • 先让我们看一个布局:
    • 这里写图片描述
      当我们看到这个布局的时候一般都会想到这里面肯定用布局嵌套。如果你使用ConstraintLayout布局的话就不需要布局嵌套,只用一个父布局就可以了。
  • Ok,ConstraintLayout这个布局的重点就是约束力,首先如果你想让最左边的头像(ImageView)放到中间的时候。那么约束它的就是它父布局的上面和下面。所以你需要在ImageView的属性里面添加这两行代码,它的含义就是自己的顶部依赖父布局的顶部,自己的底部依赖父布局的底部。这样刚好就能够是ImageView在父布局的中间了。
app:layout_constraintBottom_toBottomOf="parent"
 app:layout_constraintTop_toTopOf="parent"

然而你心中会有疑问了,这样这个头像只是在父布局的上下约束,但是它却在布局的左边。很好,这个时候要给ImageView的左边一个约束,也是相对于父布局的。这句代码的意思就是ImageView的左边和父布局的左边有一个约束。这样ImagView就在父布局的左边了。

app:layout_constraintLeft_toLeftOf="parent"

此时你还会有疑问,我是把ImageView的左边依赖父布局,但是为什么会左边的间距。因为此时,ImageView左边有父布局的约束,所以给ImageView设置android:layout_marginLeft这个属性是起作用的。我们设置ImageView距离左边距为10dp。新手刚使用ConstraintLayout的时候设置这个margin属性会不起作用,那是因为你没有给他margin方向的约束,只有先有了约束margin才会起作用。

android:layout_marginLeft="@dimen/dp_10"
 <ImageView
        android:id="@+id/iv_mine_friend_avatar"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_marginLeft="@dimen/dp_10"
        android:src="@mipmap/img1"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginStart="@dimen/dp_10"/>
  • 接下来就是显示用户的名字了,它需要在头像的上面(ImageView)的右边,并且要在ImageView的头部的上面,所以只用给它两个约束就可以了。名字的左边需要在头像的右边app:layout_constraintLeft_toRightOf="@+id/iv_mine_friend_avatar"
    名字的上面需要和头像的上面齐app:layout_constraintTop_toTopOf="@+id/iv_mine_friend_avatar"
    因为有了左边的约束所以可以设置左边的margin值android:layout_marginLeft="@dimen/dp_10"
    也可以设置上面的margin值android:layout_marginTop="@dimen/content_small"

  • 其他的布局基本上都是一样的,下面重点说一下下面item的那一条黑的线。如果你想在ConstraintLayout设置一个控件的宽度match_parent那是不允许这样写的。你只能这样写:

 app:layout_constraintLeft_toLeftOf="parent"
 android:layout_width="0dp"

你首先要设置layout_width=”0dp”,这个是必须的,然后你要让这个控件左边或者右边要约束到父布局里面。就像这条线一样,我想让它margin左边,我就只用设置这个属性就可以。app:layout_constraintLeft_toLeftOf="parent"
如果你不这样设置话,那么总是会报错。

上面的布局代码

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

    <ImageView
        android:id="@+id/iv_mine_friend_avatar"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_marginLeft="@dimen/dp_10"
        android:src="@mipmap/img1"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginStart="@dimen/dp_10"/>

    <TextView
        android:id="@+id/tv_mine_friend_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/dp_10"
        android:layout_marginTop="@dimen/content_small"
        tools:text="共享对象"
        android:textColor="#404040"
        android:textSize="@dimen/myedit_right_textsize"
        app:layout_constraintLeft_toRightOf="@+id/iv_mine_friend_avatar"
        app:layout_constraintTop_toTopOf="@+id/iv_mine_friend_avatar"
        android:layout_marginStart="@dimen/dp_10"/>

    <TextView
        android:id="@+id/tv_mine_friend_motto"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/dp_10"
        tools:text="我有句mmp一定要讲"
        android:textColor="#404040"
        android:textSize="@dimen/myedit_right_textsize"
        app:layout_constraintBottom_toBottomOf="@+id/iv_mine_friend_avatar"
        app:layout_constraintLeft_toRightOf="@+id/iv_mine_friend_avatar"
        android:layout_marginStart="@dimen/dp_10"/>

    <TextView
        android:id="@+id/tv_mine_friend_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="@dimen/dp_10"
        tools:text="刚刚"
        android:textSize="@dimen/myedit_right_textsize"
        app:layout_constraintBottom_toBottomOf="@+id/tv_mine_friend_name"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="@+id/tv_mine_friend_name"
        android:layout_marginEnd="@dimen/dp_10"/>
    <View
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_width="0dp"
        android:layout_height="0.5dp"
        android:layout_marginLeft="@dimen/dp_10"
        android:background="#404040"/>
</android.support.constraint.ConstraintLayout>

其实ConstraintLayout真的是学起来挺容易的,并且用来也超级方便,就简单介绍到这里,其实里面还有很多高端的研究,感觉会这些就基本上已经够用了。

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: SourceInsight 是一款功能强大且外观精美的源代码阅读和分析工具。它为程序员提供了一种直观、高效的方式来浏览和编辑源代码。 首先,SourceInsight 的界面设计非常吸引人。它采用了现代化的颜色搭配和清晰的图标,使得界面整洁美观,并且可以根据个人喜好进行定制。这使得长期使用工具的人也能享受到更好的视觉体验。 其次,SourceInsight 支持多种主流编程语言,包括C、C++、Java等。无论你是哪种语言的开发者,使用 SourceInsight 都能获得出色的代码浏览和编辑体验。它提供了强大的代码导航功能,能够自动识别函数、类、变量等,让你能够快速定位和浏览源代码中的各个部分。 另外,SourceInsight 还具备强大的代码分析能力。它能够自动分析源代码的结构、依赖关系和函数调用,并生成相应的图表和报告。这有助于用户更好地理解代码的组织结构和逻辑,并且能够快速定位和解决问题。 此外,SourceInsight 还提供了许多实用的辅助功能,如智能补全、自动格式化、代码模板等。这些功能能够帮助程序员提高开发效率,减少重复工作,专注于代码的编写和调试。 综上所述,SourceInsight 作为一款外观精美、功能强大的源代码阅读和分析工具,不仅让程序员能够享受到良好的使用体验,而且能够提高开发效率、加快代码分析和调试的速度。 ### 回答2: Source Insight是一款功能强大、界面美观的代码编辑器,以下是我认为它好看的几点原因。 首先,Source Insight的界面设计简洁大方。它采用了经典的三窗口布局,左侧是文件目录树,右上方是代码编辑窗口,右下方是符号窗口。这样的布局使得代码的导航和编辑更加方便,而且整个界面非常清晰,让人一目了然。 其次,Source Insight支持多种主题和配色方案。用户可以根据自己的喜好选择不同的主题来改变界面的外观,比如黑色、灰色或者其他亮丽的颜色。这样的自定义功能使得每个人都可以根据自己的喜好来打造一个独特且美观的编程环境。 再次,Source Insight提供了丰富的代码显示和分析功能。它可以高亮显示不同的语法元素,比如关键字、函数、变量等,使得代码在编辑器中看起来更加美观。它还提供了代码结构分析、跳转定义、查找引用等功能,这些功能都能提高代码的可读性和编辑的效率。 最后,Source Insight的字体渲染和代码对齐效果出众。无论是在大屏幕还是小屏幕上,代码都能以最佳的方式显示出来。它的字体显示非常清晰,让人不会感到眼睛疲劳。而且它能自动对齐代码,保证每行代码的缩进一致,使得代码更加整齐美观。 综上所述,Source Insight在界面设计、主题配色、代码显示和字体渲染等方面都很出色,让人使用起来非常舒适和愉快。它不仅是一个功能强大的代码编辑器,同时也是一个让人爱不释手的美观工具。 ### 回答3: Source Insight是一款非常实用和好看的代码编辑器。首先,它具有相当精美的界面设计,简洁而专业,让人一目了然。它的配色方案也非常舒适,不会让眼睛产生疲劳感。其次,Source Insight拥有强大的代码导航和浏览能力。它可以自动识别代码文件间的关系,使得在多个文件间切换和跳转非常方便。此外,它还能够对代码进行智能的语法高亮和代码折叠,使得代码更加清晰易读。还有一个亮点是它的代码分析功能。Source Insight可以帮助开发者快速找到代码中的错误和潜在问题,并提供相关的建议和解决方法。此外,它还支持多种编程语言,包括C/C++、Java、Python等,满足了不同开发者的需求。总之,Source Insight不仅仅是一个编辑器,更是一个能够提升开发效率的工具。无论是独立开发者还是团队协作,使用Source Insight都能够让代码的编写和维护变得更加轻松和愉快。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值