美文网首页
ConstraintLayout

ConstraintLayout

作者: 主音King | 来源:发表于2018-12-06 18:02 被阅读0次

    约束布局 增强型线性布局
    Circular positioning(圆形定位)
    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>
    

    wrap_content:enforcing constraints(强制约束)
    bt2在bt1右下侧
    app:layout_constrainedWidth="false" 时候随着bt2内容的增多,bt2横向变宽当宽度达到最大时,自动换行纵向变高,bt2会扩展到bt1中心位置下方,bt2屏幕右侧会覆盖一部分内容。
    app:layout_constrainedWidth="true" 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>
    

    Chains(链) 一组空间通过双向的约束关系链接起来
    默认样式

    app:layout_constraintHorizontal_chainStyle="spread"
    

    match_constraint dimensions(填充父窗体约束)
    1.layout_constraintWidth_min and layout_constraintHeight_min //设置最小尺寸
    2.layout_constraintWidth_max and layout_constraintHeight_max //设置最大尺寸
    3.layout_constraintWidth_percent and layout_constraintHeight_percent //设置相对于父类的百分比
    goneMargin(隐藏边距)
    -layout_goneMarginStart
    -layout_goneMarginEnd
    -layout_goneMarginLeft
    -layout_goneMarginTop
    -layout_goneMarginRight
    -layout_goneMarginBottom
    约束-百分比布局

    app:layout_constraintDimensionRatio="H,16:9"
    

    Guideline

    <android.support.constraint.Guideline
            android:layout_width="wrap_content"
            android:orientation="vertical"
            app:layout_constraintGuide_begin="100dp"
            android:layout_height="wrap_content"/>
    

    Barrier
    屏障

    <android.support.constraint.Barrier
            android:id="@+id/barrier"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:barrierDirection="right"
            app:constraint_referenced_ids="tv_name,tv_contract"/>
    

    Group

    相关文章

      网友评论

          本文标题:ConstraintLayout

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