设为首页 加入收藏

TOP

Android Material Design控件使用(一)——ConstraintLayout 约束布局(三)
2019-09-01 23:25:36 】 浏览:76
Tags:Android Material Design 控件 使用 ConstraintLayout 约束 布局
Guide_percent,指定在父控件中的宽度或高度的百分比,如0.8,表示距离顶部或者左侧的80%的距离。

  • android:orientation 设置垂直或者是水平

  • Guideline是隐藏的,不用我们进行多余的设置,虽然外面在预览面板可以死看到它的存在

    例子:使一个按钮的长度占满屏幕一半

        <android.support.constraint.Guideline
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/guidelineBegin"
            app:layout_constraintGuide_percent="0.5"
            android:orientation="vertical"/>
    
        <Button
            android:text="Button"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:id="@+id/button"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/guidelineBegin"
            app:layout_constraintTop_toTopOf="parent" />

    Radio 比例 属性

    方便快捷调整控件的宽高比,结合Guideline使用更佳

    例子:

    app:layout_constraintDimensionRatio

    想要宽度与总布局一样,但高度是宽度的三分之一

    宽高比为3:1

        <Button
            android:layout_width="match_parent"
            android:layout_height="0dp"
            app:layout_constraintDimensionRatio="3:1"/>
            
            

    Chain 链 属性

    可以把这个当做成一个加强版的LinearLayout

    由上图,很好的知道了A与B的约束,A的左边是父控件的左边,右边则是B,B的左边是A,B的右边的是父控件的右边

    chainstyle有三种属性,分别是spread,spread_inside,pack,效果如下图

    • spread 元素将展开(默认)

    • spread_inside 元素展开,但链的端点不会展开

    • pack 链中的元素将包裹在一起。子控件的水平或垂直方向的偏置bias属性会影响包裹中元素的位置

    剩下的两种则是通过添加属性实现的

    weighted chain是在spread chain的基础上,而packed chain with bias则是在weight chain的基础上

    style为spread的,使用layout_constraintHorizontal_weightlayout_constraintVertical_weight来设置weigh权重

    style为pack的,使用layout_constraintHorizontal_biaslayout_constraintVertical_bias进行偏移设置,属性值0-1,也是百分比,改第一个元素

    这里需要说明的是,除了水平,还可以是垂直排列,不过,不能通过属性来更改,而是通过约束来更改,把左边改为顶端,右边改为底部

    如果是水平的,使用属性layout_constraintHorizontal_chainStyle进行chainstyle属性的更改

    垂直的则是使用layout_constraintVertical_chainStyle

    例子:

    三个按钮平分宽度(只需要将宽度设置为0dp就可以达到目的)

        <Button
            android:id="@+id/btn"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Button"
            app:layout_constraintEnd_toStartOf="@id/btn1"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"/>
        <Button
            android:id="@+id/btn1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Button"
            app:layout_constraintStart_toEndOf="@id/btn"
            app:layout_constraintEnd_toStartOf="@id/btn2"/>
        <Button
            android:id="@+id/btn2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Button"
            app:layout_constraintStart_toEndOf="@id/btn1"
            app:layout_constraintEnd_toEndOf="parent"/>
    首页 上一页 1 2 3 下一页 尾页 3/3/3
    】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
    上一篇Android-蓝牙自动配对与隐藏对话框 下一篇读懂 Gradle 的 DSL

    最新文章

    热门文章

    Hot 文章

    Python

    C 语言

    C++基础

    大数据基础

    linux编程基础

    C/C++面试题目