Android 布局之约素布局

CinstraintLayout 布局就是约束布局,是现在安卓开发的主流布局,其中对组件的位置有上下左右对齐和在上下左右变以及横纵向比例分布,看起来是不是很熟悉?

 其实就是相对布局的一个升级版,

layout_contrainTop_toBottomof:其实就是当前组件的上边与后面加的ID的那个组件的下面对齐;

这不是就像是相对布局的 layout_below?  只是一个小小的升级了而已,而且呢可以减少我们的布局嵌套哦,能方便不少

 <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        tools:ignore="MissingConstraints"

        app:layout_constraintTop_toBottomOf="@id/tv_class"
        android:layout_marginTop="20dp"

        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/lyFrist"
            app:layout_constraintTop_toTopOf="parent"
            android:orientation="horizontal">
            <TextView
                android:id="@+id/tvName"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                app:layout_constraintLeft_toLeftOf="parent"
                android:layout_weight="2"
                android:gravity="right"
                android:text="姓名:"
                android:textSize="40dp"
                />
            <EditText
                android:layout_width="0dp"
                android:layout_height="50dp"
                android:layout_weight="3"
                app:layout_constraintLeft_toRightOf="@id/tvName"
                />
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/lySeccond"
            app:layout_constraintTop_toBottomOf="@id/lyFrist"
            android:layout_marginTop="30dp"
            android:orientation="horizontal">
            <TextView
                android:id="@+id/tvPasswprd"
                android:gravity="right"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="2"
                android:text="班级:"
                android:textSize="40dp"
                />
            <EditText
                android:layout_width="0dp"
                android:layout_height="50dp"
                android:layout_weight="3"
                />
        </LinearLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toBottomOf="@id/lySeccond"
            android:layout_marginTop="30dp"
            tools:ignore="NotSibling">

            <Button
                android:id="@+id/btLogin"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
               android:layout_toLeftOf="@id/tvMiddle"
                android:text="登陆"
                android:textSize="40dp"/>

            <TextView
                android:layout_width="30dp"
                android:layout_height="50dp"
                android:id="@+id/tvMiddle"
                android:layout_centerInParent="true"/>

            <Button
                android:id="@+id/btCreate"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/tvMiddle"
                android:text="注册"
                android:textSize="40dp"/>
        </RelativeLayout>




    </androidx.constraintlayout.widget.ConstraintLayout>

让我们来解析一下这段代码,这里呢小编还是用来几个布局的嵌套,目的是为了让大家能复习一下之前的知识哈,绿色部分下面的一段才是上面的代码,大布局用的是约束布局,班级和姓名嵌套了线性布局,这里有一个点gravity这个属性的意思是内容靠的方向,而有一个layout_gravity的意思是该控件对于布局的位置。

线性布局里面我还用到了一个权重的技巧,本来吧想着当手机横屏的时候不会乱套,结果精心设计一番发现手机横屏居然不旋转,可能是小编忽略了什么设置,哈哈,可以说是没看到自己设计的成果,不过大致上样子还是挺像,供大家了解约素布局还是够了。

还有一个水平和垂直倾向的属性,不过这里没有用到,因为小编试了一下,结果好像不是很近人意,大家有兴趣可以试一试,就是layout-constraintHorzontsl_bias 和layout-constraintVertical_bias 后面跟的呢是一个0~1的数用来表示占这一行(列)的多少。

约束布局为什么这么流行,其实还有很多功能呢,大家可以回去多试一试,你会发现很多有趣的布局方式。 就到这吧,再见。。。

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

编程学渣ズ

谢谢老板

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值