2025.5.16
学习内容
LinearLayout 介绍:LinearLayout 是线性布局,子视图可以按水平或垂直方向排列。通过android:orientation属性设置排列方向,horizontal为水平排列,vertical为垂直排列。
使用 LinearLayout 布局:在布局文件中以 LinearLayout 作为根布局,添加多个子视图,如 TextView、Button 等。例如,垂直排列的 LinearLayout 布局代码如下:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/text_view"
android:text="文本1"
android:textSize="20sp"
android:textColor="#000000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/button"
android:text="按钮"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
权重设置:使用android:layout_weight属性为子视图分配权重,实现按比例分配空间。例如,两个 TextView 设置不同的权重,可使它们在水平或垂直方向上按比例占据空间。
收获
掌握了 LinearLayout 的布局方式,学会通过设置方向和权重来合理排列子视图。在实际布局过程中,发现权重设置能很好地解决不同组件的空间分配问题,对 Android 的布局管理有了初步的实践经验。

浙公网安备 33010602011771号