• ConstraintLayout使用 笔记总结


    场景:实现文本对齐。属性:layout_constraintBaseline_toBaselineOf

    • Baseline指的是文本基线:比如两个TextView的高度不一致,但是又希望他们文本对齐,这个时候就可以使用layout_constraintBaseline_toBaselineOf

    角度定位。属性:app:layout_constraintCircle ; app:layout_constraintCircleAngle ;app:layout_constraintCircleRadius

    • 角度定位指的是可以用一个角度和一个距离来约束两个空间的中心。

        <TextView
                android:id="@+id/TextView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
      
        <TextView
            android:id="@+id/TextView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintCircle="@+id/TextView1"       //参照物
            app:layout_constraintCircleAngle="120"             //角度
            app:layout_constraintCircleRadius="150dp" />       //两点之间的距离
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12

    属性:goneMargin:用于约束的控件可见性被设置为gone的时候使用的margin值。在参照物被设置为gone的时候见效

        layout_goneMarginStart
        layout_goneMarginEnd
        layout_goneMarginLeft
        layout_goneMarginTop
        layout_goneMarginRight
        layout_goneMarginBottom
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    属性:偏移:layout_constraintHorizontal_bias ; layout_constraintVertical_bias

    • 区别与margin。这两个的取值是倍数:0~1.

    属性: 尺寸约束的细节:

    • 使用wrap_content的时候,以使用下列属性来控制最大、最小的高度或宽度:
      • android:minWidth 最小的宽度
      • android:minHeight 最小的高度
      • android:maxWidth 最大的宽度
      • android:maxHeight 最大的高度
        当ConstraintLayout为1.1版本以下时,使用这些属性需要加上强制约束,如下所示:
      • app:constrainedWidth=”true”
      • app:constrainedHeight=”true”
    • 使用0dp,不推荐使用match_parent

    属性:layout_constraintDimensionRatio 宽高比

    • 比如正方形: app:layout_constraintDimensionRatio=“1:1”
    • 可以在前面加W或H,分别指定宽度或高度限制。 例如:
      app:layout_constraintDimensionRatio=“H,2:3” //指的是 高:宽=2:3
      app:layout_constraintDimensionRatio=“W,2:3” //指的是 宽:高=2:3

    属性:layout_constraintWidth_percent 设置在布局内部的占比。比如实现在布局内部的占比一半并且居中

    <android.support.constraint.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">
    
        <TextView
            android:id="@+id/button"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="textView"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintWidth_percent="0.5"/>
    
    </android.support.constraint.ConstraintLayout>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    链: 当控件与控件之间相互约束的话,就可以将它们视为一条链。

    • 属性:layout_constraintHorizontal_chainStyle:改变整条链的样式,三种:
      • app:layout_constraintHorizontal_chainStyle=“packed” //打包起来:去掉了两两之间的间距
      • app:layout_constraintHorizontal_chainStyle=“spread” //(默认) 展开,两两之间平分间距
      • app:layout_constraintHorizontal_chainStyle=“spread_inside”// 展开,两两之间平分间距但是链的两端会贴边
        ps:垂直方向也有对应的三种

    控件:androidx.constraintlayout.widget.Barrier 屏障,或者说参照线。

    • 据其包含的组件在某个方向最远的边缘确定。

    • 用于被参照物不确定(比如说宽高不确定,或者说显示隐藏不确定)的情况。将参照物进行打包。

      • 配合使用的属性:app:constraint_referenced_ids : 存放引用的参照物,用,隔开

          <androidx.constraintlayout.widget.Barrier
              android:id="@+id/barrier_end"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              app:constraint_referenced_ids="fl_screen_normal,btn_save"
              app:layout_constraintBottom_toBottomOf="parent"
              app:layout_constraintEnd_toEndOf="parent" />
        
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7

    控件:androidx.constraintlayout.widget.Group: 把多个控件打包,方便控制一整组控件的显示隐藏
    - 配合使用的属性:app:constraint_referenced_ids : 存放引用的控件,用,隔开

  • 相关阅读:
    NPS:使用 Windows NPS Server 部署 802.1X 无线认证(3)
    项目的打包发布
    Spread for ASP.NET 16.0 中文就是你喜好 Spread.NET
    python logging
    OkHttpClient实例
    pytorch hook机制
    javaer你还在手写分表分库?来看看这个框架怎么做的 干货满满
    FreeSWITCH 1.10.10 简单图形化界面11 - 简单封装一下JSSIP
    git创建本地分支并提交到远程
    Python:利用Python读取txt、csv、xlsx、doc、json、parquet等各种数据文件类型的多种方法总结
  • 原文地址:https://blog.csdn.net/wzj_what_why_how/article/details/125625579