Androidx学习笔记(6)--常见布局--线性布局

本文介绍了Androidx中的线性布局LinearLayout,包括其排列方向的设定、对齐方式以及权重的应用。线性布局允许组件按垂直或水平方向排列,并在特定条件下支持对齐方式。权重属性对于灵活分配空间尤为关键。

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

线性布局

LinearLayout
  • 指定各个节点的排列方向

    android:orientation="horizontal"
    
  • 设置右对齐

    android:layout_gravity="right"
    
  • 当竖直布局时,只能左右对齐和水平居中,顶部底部对齐竖直居中无效
  • 当水平布局时,只能顶部底部对齐和竖直居中
  • 使用match_parent时注意不要把其他组件顶出去
  • 线性布局非常重要的一个属性:权重

    android:layout_weight="1"
    


权重设置的是按比例分配剩余的空间,并且要设置layout_width或layout_height要为0
  • 有一个布局方向,水平或者竖直
  • 在竖直布局下,左对齐、右对齐,水平居中生效
  • 在水平布局下,顶部对齐、底部对齐、竖直居中生效
  • 权重:按比例分配屏幕的剩余宽度或者高度

示例:



布局代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <LinearLayout 
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="horizontal"
        >
        <TextView 
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:background="#ff0000"
            />
        <TextView 
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:background="#ffffff"
            />
        <TextView 
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:background="#000000"
            />
        <TextView 
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:background="@android:color/darker_gray"
            />
    </LinearLayout>
    <LinearLayout 
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="vertical"
        >
        <TextView 
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:background="#00ff00"
            />
        <TextView 
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:background="@android:color/darker_gray"
            />
        <TextView 
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:background="#000000"
            />
        <TextView 
            android:layout_weight="1"
            android:layout_width="match_parent"            
            android:layout_height="0dp"
            android:background="#ffcc0000"
            />
    </LinearLayout>
</LinearLayout>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值