安卓开发实战(2)之Android线性布局LinearLayout

系列文章目录



前言

线性布局就是字面意思呈垂直或者水平对view进行布局


一、垂直

就是代码块第五行将 android:orientation=“vertical”
即可设置此模块内下的view为垂直分布

<?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">

<TextView
    android:layout_width="600dp"
    android:layout_height="200dp"
    android:background="@color/black"
    android:text="菜单1"
    android:textColor="@color/white"
    android:textSize="80sp"
    />
    <TextView
        android:layout_width="500dp"
        android:layout_height="200dp"
        android:background="#4593ec"
        android:text="菜单2"
        android:textColor="@color/white"
        android:textSize="80sp"
        />
    <TextView
        android:layout_width="400dp"
        android:layout_height="200dp"
        android:background="#7DB"
        android:text="菜单3"
        android:textColor="@color/white"
        android:textSize="80sp"
        />



</LinearLayout>

实际效果:
在这里插入图片描述

二、水平

就是代码块第五行将 android:orientation=“horizontal”
即可设置此模块内下的view为水平分布

<?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="horizontal">

<TextView
    android:layout_width="100dp"
    android:layout_height="500dp"
    android:background="@color/black"
    android:text="菜单1"
    android:textColor="@color/white"
    android:textSize="80sp"
    />
    <TextView
        android:layout_width="200dp"
        android:layout_height="500dp"
        android:background="#4593ec"
        android:text="菜单2"
        android:textColor="@color/white"
        android:textSize="80sp"
        />
    <TextView
        android:layout_width="300dp"
        android:layout_height="500dp"
        android:background="#7DB"
        android:text="菜单3"
        android:textColor="@color/white"
        android:textSize="80sp"
        />



</LinearLayout>

实际效果:
在这里插入图片描述

三、权重

权重就是如果设置 android:layout_height=“0dp”,可以对所有的view进行均分,权重大小就是比例,比如三个view权重为,2,2,1。且 android:layout_height均为"0dp",则该三个view以2:2:1的比例平分页面大小。
权重代码:android:layout_weight="2"

在这里插入代码片<?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">

<TextView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="2"
    android:background="#ff1323"
    />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2"
        android:background="@color/white"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#cd526879"
        />



</LinearLayout>

实际效果:
在这里插入图片描述

总结

比较简单好理解0.0

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

czDemo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值