Android LinearLayout 布局详解:用法、属性和示例
LinearLayout 是 Android 中的一种布局容器,用于在水平或垂直方向排列子视图。在 XML 文件中使用 LinearLayout 布局时,必须声明这个元素。示例如下:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- 子视图的布局 -->
</LinearLayout>
其中,android:layout_width 和 android:layout_height 属性用于指定 LinearLayout 的宽度和高度。xmlns:android 属性是命名空间声明,用于指定 Android 的 XML 命名空间。
在 LinearLayout 中添加子视图时,可以使用 LayoutParams 类型来指定子视图的布局参数,例如:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/my_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text='Hello, World!'
android:layout_gravity="center"/>
</LinearLayout>
在这个示例中,TextView 是 LinearLayout 的子视图,使用了 android:layout_gravity 属性将其居中显示。值得注意的是,在 Android 中,元素的属性通常使用驼峰式命名,例如 layout_width 和 layout_height。
原文地址: https://www.cveoy.top/t/topic/nYiT 著作权归作者所有。请勿转载和采集!