2025年Android 布局之约素布局

Android 布局之约素布局CinstraintLa 布局就是约束布局 是现在安卓开发的主流布局 其中对组件的位置有上下左右对齐和在上下左右变以及横纵向比例分布 看起来是不是很熟悉 其实就是相对布局的一个升级版 layout contrainTop toBottomof

大家好,我是讯享网,很高兴认识大家。

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的数用来表示占这一行(列)的多少。

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

 

 

小讯
上一篇 2025-04-11 09:50
下一篇 2025-04-08 14:17

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/55278.html