首先,打开工程,点击app -> src -> main -> res -> layout -> activity_main.xml
将 androidx.constraintlayout.widget.ConstraintLayout 替换成LinearLayout
helloword 从中间变成左上角了
最常用属性
android: id 标识
android: layout_width 宽度
android: layout_height 高度
android: background 背景
android: layout_margin 外边距
android:layout_padding 内边距
android: orientation 方向
android: layout_weight 权重
此时,将这个控件这一段粘贴复制
将复制后的Helloword 改成你好,
点击运行可以看到,与Helloword 处于水平排列的状态
LinearLayout
是 Android 开发中的一种布局方式,用于在屏幕上以线性方式排列子视图。它可以是垂直或水平排列,具体取决于设置的方向属性。
当我们在这里加一句android:orientation="vertical" ,子视图效果就变成从上到下排列
将 vertical 换成 horizontal 子视图就变回水平排列了
使用场景:适合简单的布局需求,比如表单、按钮组等。
LinearLayout
是 Android 中最常用的布局之一,因其简单易用而广受欢迎。在 Android 开发中,使用 LinearLayout
可以方便地创建线性排列的用户界面。