Android LinearLayout线性布局

本文详细探讨了Android中的LinearLayout,包括其工作原理、属性解析、垂直与水平排列方式,以及如何在XML和代码中实现布局。同时,还讨论了LinearLayout与其他布局管理器的比较和在复杂布局中的使用策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

线性布局LinearLayout是一个视图容器,用于使所有子视图在单个方向保持对齐
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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">

    <TextView
        android:layout_width="200dp"
        android:layout_height="90dp"
        android:background="#4593Ec"
        android:gravity="center"
        android:text="1"
        android:textColor="@color/white"
        android:textSize="25sp"/>

    <TextView
        android:layout_width="100dp"
        android:layout_height="90dp"
        android:background="#7DB145"
        android:gravity="center"
        android:text="2"
        android:textColor="@color/white"
        android:textSize="25sp"/>

    <TextView
        android:layout_width="80dp"
        android:layout_height="90dp"
        android:background="#F10000"
        android:gravity="center"
        android:text="3"
        android:textColor="@color/white"
        android:textSize="25sp"/>

</LinearLayout>

android:orientation="vertical" 垂直方向下(图1)
android:orientation="horizontal" 水平方向下(图2)

在这里插入图片描述
在这里插入图片描述

布局权重:通过给子视图设置权重值,来分配子视图所占空间的权重(比例)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="#3c3f41"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2"
        android:background="#ff5722"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#4795ec"/>

</LinearLayout>

在这里插入图片描述

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值