Android LinearLayout 线性布局

示例

LinearLayout是一种ViewGroup将其子级排列在单列或单行中的。可以通过调用方法setOrientation()或使用xml属性来设置方向android:orientation。

  1. 垂直方向:android:orientation="vertical"

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/app_name" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@android:string/cancel" />

</LinearLayout>

这是一个屏幕截图,看起来像这样:

  1. 水平方向:android:orientation="horizontal"

     <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/app_name" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@android:string/cancel" />

该LinearLayout还支持分配权重,以个别儿童与android:layout_weight属性。