Android XML 布局基础(五)线性布局 - LinearLayout

617 阅读1分钟

一、简介

  • 多种 Layout 布局是可以嵌套组合使用的。

  • LinearLayout 是一个视图容器,用于使所有子视图在单个方向(垂直或水平)保持对齐,可使用 android:orientation 属性指定布局方向。

  • 测试代码

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <!-- 由于最外层不是 Layout,则这里为根布局 -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:background="#ffc">
            <!-- 子视图列表 -->
            <View
                android:layout_width="80dp"
                android:layout_height="80dp"
                android:background="#cff"/>
            <View
                android:layout_width="80dp"
                android:layout_height="80dp"
                android:background="#fcf"/>
            <View
                android:layout_width="80dp"
                android:layout_height="80dp"
                android:background="#cff"/>
            <View
                android:layout_width="80dp"
                android:layout_height="80dp"
                android:background="#fcf"/>
            <View
                android:layout_width="80dp"
                android:layout_height="80dp"
                android:background="#cff"/>
        </LinearLayout>
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

二、属性

  • android:orientation="horizontal"

    测试代码同上,修改一下方向代码即可。

    image.png

  • android:orientation="vertical"

    测试代码同上,修改一下方向代码即可。

    image.png

  • android:layout_weight="1"

    通过给子视图设置权重值,来分配子视图所占空间的权重(比例),如图三个子视图权重分别设置为 1,则均分页面空间。

     <?xml version="1.0" encoding="utf-8"?>
     <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:app="http://schemas.android.com/apk/res-auto"
         xmlns:tools="http://schemas.android.com/tools"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         tools:context=".MainActivity">
    
         <!-- 由于最外层不是 Layout,则这里为根布局 -->
         <LinearLayout
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:orientation="horizontal"
             android:background="#ffc">
             <!-- 子视图列表 -->
             <View
                 android:layout_width="80dp"
                 android:layout_height="80dp"
                 android:layout_weight="1"
                 android:background="#cff"/>
             <View
                 android:layout_width="80dp"
                 android:layout_height="80dp"
                 android:layout_weight="1"
                 android:background="#fcf"/>
    
             <View
                 android:layout_width="80dp"
                 android:layout_height="80dp"
                 android:layout_weight="1"
                 android:background="#cff" />
         </LinearLayout>
    
     </androidx.constraintlayout.widget.ConstraintLayout>
    

    image.png

    image.png

    也可以组合使用

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <!-- 由于最外层不是 Layout,则这里为根布局 -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:background="#ffc">
            <!-- 子视图列表 -->
            <View
                android:layout_width="80dp"
                android:layout_height="80dp"
                android:background="#cff"/>
            <View
                android:layout_width="80dp"
                android:layout_height="80dp"
                android:layout_weight="2"
                android:background="#fcf"/>
    
            <View
                android:layout_width="80dp"
                android:layout_height="80dp"
                android:layout_weight="0.5"
                android:background="#cff" />
        </LinearLayout>
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    image.png