美文网首页
ConstraintLayout使用

ConstraintLayout使用

作者: 主音King | 来源:发表于2019-11-18 10:37 被阅读0次

    ConstraintLayout约束布局

    Google大力推广布局方式。
    高性能:基本一层布局搞定,不用嵌套多层布局
    功能强大:百分比布局、设置自身宽高比例,各种辅助组件
    可拖拽编辑:和ios拖拽布局很相似
    缺点:用了约束布局后,再也无法回到以前的布局方式了,重度依赖

    基本布局方式

    app:layout_constraintStart_toStartOf -->自身左边与目标左边对齐
    

    偏移量bias

    居中显示,要往左边靠一点。可以通过bias来实现

    app:layout_constraintHorizontal_bias-->水平方向的偏移,取值[0f,1.0f](0为靠向最左边,1为靠到最右边)
    

    margin

    margin生效,必须同一方向上的约束,要marginBottom就必须layout_constraintBottom_toxxxof

    goneMargin(隐藏边距)

    -layout_goneMarginStart
    -layout_goneMarginEnd
    -layout_goneMarginLeft
    -layout_goneMarginTop
    -layout_goneMarginRight
    -layout_goneMarginBottom

    大小设置

    约束布局下三种大小设置模式

    spread, wrap,percent;

    app:layout_constraintWith_default-->设置
    

    注意:使用约束布局的大小设置,需要先将要设置的宽或高指定0dp

    spread:默认

    表示在约束条件下的最大尺寸

    wrap模式

    自适应大小,不会超过约束条件下的最大尺寸
    传统的wrap_content模式,约束布局有额外的属性,使得它也能保留约束条件

    android:layout_width="0dp"// 对应比例的地方为0
    app:layout_constrainedWidth="true"此时与wrap模式一样的效果
    ###percent模式
    以父布局的百分比作为自身大小,通过layout_constraintWith_percent设置比例大小[0f,1.0f]
    

    指定宽高比ratio

    使其生效必须设置宽或高为0dp

    app:layout_constraintDimensionRatio-->指定view自身的宽高比例。
    

    如果宽高两项为0dp的话,则最终尺寸设置符合约束的最大尺寸,同时保持设置的比例。有时候不是我们想要的,可以指定宽或者高(H,W)哪一边约束条件来确定尺寸。

    app:layout_constraintDimensionRatio="W,2:1"-->宽对按照比例计算最终尺寸。
    app:layout_constraintDimensionRatio="H,2:1"-->则相反
    

    最大和最小值(max/min)

    app:layout_constraintHeight_min
    

    链布局(chain)

    可以快速实现等分布局,还可以实现类似LinearLayout布局的weight比重功能。
    约束链三种模式:
    spread:view之间均匀分布(默认)
    spread_inside:除了约束链的头部和尾部贴在两边,其余均匀分布
    packed:所有view紧贴在一起,默认居中

    注意:layout_constraintLeft_toLeftOf(正常)和layout_constraintStart_toStartOf(不正常)在布局表现不一样;以此类推

    image.png

    在链上设置权重

    app:layout_constraintHorizontal_weight--->权重
    

    圆形布局

    app:layout_constraintCircle-->圆心,某个view的id
    app:layout_constraintCircleRadius-->半径
    app:layout_constraintCircleAngle-->角度,值[0,360],0是正上方
    

    button2在button1正上方(半径100dp)向右偏移45度

    <android.support.constraint.ConstraintLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent" 
            android:layout_height="match_parent"
            tools:context=".MainActivity">
       <Button
                android:id="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button ONE"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent"/>
    
        <Button 
                android:id="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:layout_constraintCircle="@+id/button1"
                app:layout_constraintCircleRadius="100dp"
                app:layout_constraintCircleAngle="45"
                android:text="Button TWO"/>
    </android.support.constraint.ConstraintLayout>
    

    bt2在bt1右下侧

    <android.support.constraint.ConstraintLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context=".MainActivity">
    
        <Button
                android:id="@+id/bt1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="16dp"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constrainedWidth="false"
                android:text="button one"/>
    
        <Button
                android:id="@+id/bt2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:layout_constraintLeft_toRightOf="@+id/bt1"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/bt1"
                android:text="Button TWO"/>
    
    </android.support.constraint.ConstraintLayout>
    

    app:layout_constrainedWidth="false" 时候随着bt2内容的增多,bt2横向变宽当宽度达到最大时,自动换行纵向变高,bt2会扩展到bt1中心位置下方,bt2屏幕右侧会覆盖一部分内容。
    app:layout_constrainedWidth="true" bt2会扩展到bt1右侧位置下方,屏幕右侧不会覆盖一部分内容。

    辅助组件

    Group:控制可见
    Guideline:辅助线
    Barrier:边界范围

    Group控制可见性

    Group虚拟视图,通过constraint_refrenfed_ids放置里面,统一同时控制这些view的课件性。

    <androidx.constraintlayout.widget.Group
            android:id="@+id/mGroup"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:constraint_referenced_ids="img1,img2"
            />
    // 代码中控制可见性
    mGroup.visibility = View.VISIBLE
    

    辅助线

    Guideline虚拟辅助线,水平、垂直,帮助定位。
    两个view在屏幕中间一左一右,可以通过在屏幕中间放置一个虚拟辅助线,两个view分布约束到辅助线的两侧。

    android:orientation-->设置垂直还是水平
    app:layout_constraintGuide_percent-->通过百分比设置位置,取值[0f,1.0f]或者[0%,100%]
    app:layout_constraintGuide_begin-->设置相对start/top的偏移量,dp
    app:layout_constraintGuide_end-->设置相对end/bottom的偏移量,dp
    
    <androidx.constraintlayout.widget.Guideline
            android:id="@+id/guideline"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            app:layout_constraintGuide_percent="0.5"
            />
    
        <ImageView
            android:id="@+id/img1"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:src="@drawable/img1"
            android:scaleType="centerCrop"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/guideline"
            android:layout_marginTop="20dp"
            android:layout_marginEnd="50dp"
            />
    
        <ImageView
            android:id="@+id/img2"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:src="@drawable/img2"
            android:scaleType="centerCrop"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toEndOf="@+id/guideline"
            android:layout_marginTop="20dp"
            android:layout_marginStart="50dp"
            />
    

    Barrier:获取边界范围

    Barrier可以获取多个约束view的边界,可以获得所包含的多个view的最左最右等边界。
    左边是text1和text2,另外一个view必须放在这两个text的右边。barrier可以动态获取右侧。解决问题

    <androidx.constraintlayout.widget.Barrier
            android:id="@+id/barrier"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:constraint_referenced_ids="text1,text2"
            app:barrierDirection="end"
            />
        <ImageView
            android:id="@+id/img1"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:src="@drawable/img1"
            android:scaleType="centerCrop"
            app:layout_constraintTop_toTopOf="@id/text1"
            app:layout_constraintBottom_toBottomOf="@id/text2"
            app:layout_constraintStart_toEndOf="@id/barrier"
            android:layout_marginStart="50dp"
            />
    

    技巧 宽度限制layout_width="0dp"(高度上同理)

    场景1:设置view2的宽度和view1的宽度一样。设置view2为
    android:layout_width="0dp"
    layout_constraintStart_toStartOf=view1;
    layout_constraintEnd_toEndOf=view1;

    场景2:设置百分比宽为0.7:
    android:layout_width="0dp"
    layout_constraintWidth_percent="0.7"

    merge 在ConstraintLayout使用

    主布局activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.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=".MainActivity2">
        <TextView
            android:id="@+id/tvTest1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            android:layout_marginTop="50dp"
            android:text="测试布局"
            android:textSize="26sp"
            />
        <include layout="@layout/include_test"/>
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    次布局:include_test.xml

    <?xml version="1.0" encoding="utf-8"?>
    <merge 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"
        tools:parentTag="androidx.constraintlayout.widget.ConstraintLayout">
        <TextView
            android:id="@+id/tvTest2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toBottomOf="@+id/tvTest1"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            android:text="测试布局"
            android:textSize="26sp"
            />
    </merge>
    

    注意:
    主布局的include不需要加id和宽高限制。
    次布局include中的merge不需要宽高限制。需要添加tools:parentTag="androidx.constraintlayout.widget.ConstraintLayout"约束,方便在xml预览主视图。
    merge中的布局可以直接引用主视图的布局进行位置定位。

    相关文章

      网友评论

          本文标题:ConstraintLayout使用

          本文链接:https://www.haomeiwen.com/subject/vbrvictx.html