Android线性布局教程

线性布局(LinearLayout)是Android中最常用的布局之一,它允许您在水平或垂直方向上排列视图。以下是一些关于线性布局的基础知识和使用方法。

线性布局的基本属性

  • orientation: 设置布局的方向,可以是horizontal(水平)或vertical(垂直)。
  • weight: 设置子视图的权重,当父视图的空间不足以容纳所有子视图时,权重较大的视图将占据更多的空间。
  • gravity: 设置子视图在父视图中的对齐方式。

实例

以下是一个简单的线性布局示例:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:gravity="center">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello, World!"
        android:layout_margin="16dp"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click Me"
        android:layout_margin="16dp"/>

</LinearLayout>

扩展阅读

如果您想了解更多关于Android布局的知识,可以访问我们的Android布局教程

Android Linear Layout