美文网首页
约束布局常用属性和辅助组件

约束布局常用属性和辅助组件

作者: 镌刻心语 | 来源:发表于2022-11-03 10:30 被阅读0次

    一、常用属性

    1.相对定位属性:layout_constraint X _to Y Of

    一句话描述:当前控件的X边界 - 在 - 目标控件的Y边界
    layout_constraintTop_toBottomOf
    layout_constraintTop_toTopOf
    layout_constraintBottom_toBottomOf
    layout_constraintBottom_toTopOf
    layout_constraintStart_toEndOf
    layout_constraintStart_toStartOf
    layout_constraintEnd_toEndOf
    layout_constraintEnd_toStartOf
    layout_constraintLeft_toLeftOf
    layout_constraintLeft_toRightOf
    layout_constraintRight_toLeftOf
    layout_constraintRight_toRightOf
    layout_constraintBaseline_toBaselineOf
    一个控件最少需要 两个 相对位置属性 来确定它的具体位置

    2.尺寸控制属性:0dp

    宽度和高度如果设置为0dp的话,控件就会撑满能够达到的最大宽高。
    类似于线性布局里面加了权重的View。

    3.居中和偏移属性 :bias

    举例
    app:layout_constraintHorizontal_bias="0.3"
    这边bias=0.3的意思是,左边距离是左右总间距的30%。
    假如bias=1的话,控件将紧靠右边界。
    假如bias>1的话,控件将超出右边界。
    (垂直情况下bias的值是上边距占比)

    4.链条布局属性 :Chains

    同一个方向(水平或者垂直)上的多个子 View 提供一个类似群组的概念。
    该属性添加到群组View的头部子View即可:水平群组,最左边的为头, 垂直群组最上面为头。
    layout_constraintHorizontal_chainStyle = "packed"
    layout_constraintHorizontal_weight
    layout_constraintVertical_chainStyle
    layout_constraintVertical_weight
    具体的Style值 有三种:
    packed
    spread
    spread_inside

    二、辅助控件

    1.参照线控件Guideline

    参照线就是一个用来对齐其他视图且运行时隐藏的参照视图
    app:orientation="vertical"声明参照线是垂直还是水平方向
    app:layout_constraintGuide_begin="41dp"表示参照线离父布局ConstraintLayout的起始位置为41dp,再次声明,是start而不是left。
    app:layout_constraintGuide_end=""表示相对于右边缘的距离,
    对于百分比参照线来说,使用app:layout_constraintGuide_percent="0.5"来描述百分比的偏移量。

    2.分组控件:Group

    组的作用是可将多个控件一起隐藏或显示。
    假如在代码中实例化group控件,然后调用显示隐藏方法。
    app:constraint_referenced_ids 里面对应id的view 会一起隐藏或者显示。

    3.占位符控件:Placeholder

    控件需要移动时的终点
    下图例子中Placeholder位于屏幕中心。
    <androidx.constraintlayout.widget.Placeholder
    android:id="@+id/placeholder"
    android:layout_width="100dp"
    android:layout_height="100dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

    按钮点击后:将左上角图片设置到占位符上。
    fun click(view: View) {
    //设置动画
    TransitionManager.beginDelayedTransition(constraintLayout)
    //占位填充
    placeholder?.setContentId(R.id.logo)
    }

    4.屏障控件:Barrier

    <androidx.constraintlayout.widget.Barrier
    android:id="@+id/barrier"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:barrierDirection="right"
    app:constraint_referenced_ids="textView1,textView2"/>

    控件A,控件B长度自适应,需要控件C一直在右边,不管A和B谁长谁短 C均在右边。
    app:constraint_referenced_ids = "id1,id2 " 对应id的控件都会被屏障控件隔开.
    app:barrierDirection="right" 这个属性的意思是:屏障位于指定控件们的右边。

    相关文章

      网友评论

          本文标题:约束布局常用属性和辅助组件

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