【Android】线性布局(LinearLayout)最全解析

一、LinearLayout概述

线性布局(LinearLayout)主要以水平或垂直方式来排列界面中的控件。并将控件排列到一条直线上。在线性布局中,如果水平排列,垂直方向上只能放一个控件,如果垂直排列,水平方向上也只能方一个控件。

在这里插入图片描述
使用线性布局,需要将布局节点改成LinearLayout,基本格式如下:

 
<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">
 ....
    
</LinearLayout>

二、LinearLayout常用属性

2.1 orientation属性

在线性布局中,控件排列有水平和垂直两个方向,控件排列方向由android:orientation属性来控制,该属性需要加在LinearLayout标记的属性中。

在这里插入图片描述
在这里插入图片描述
从上图可以看出,将orientation属性值设置成为horizontal,控件将从水平方向从左往右排列,将orientation属性值设置成为vertical控件将从垂直方向从上往下排列。

2.2 gravity属性

线性布局的控件默认是从左往右排列或从上往下排列,如果想让线性布局中的控件排列对齐右边缘或者底部,可以用gravity属性控制。
在这里插入图片描述

不过该属性值并不是只有在LinearLayout中才能使用,其他的布局用该属性同样能生效。

2.3 layout_weight属性

LinearLayout中另外一个常用的属性是layout_weight,该属性需要加在LinearLayout的子控件中。其作用是分配线性布局中的剩余空间到该控件上。

如下图所示,在控件没有添加layout_weight属性时,控件未占满线性布局的区域会空出来。
在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="400dp"
    android:layout_height="300dp"
    android:background="@color/teal_200"
    android:orientation="horizontal"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:text="Button" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>

给控件button2加上android:layout_weight="1"后,会把线性布局剩余空间全部占满。

在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="400dp"
    android:layout_height="300dp"
    android:background="@color/teal_200"
    android:orientation="horizontal"
    tools:context=".MainActivity">
    <Button
        android:id="@+id/button"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:text="Button" />
    <Button
        android:id="@+id/button2"
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>

如果给button1button2都加上android:layout_weight="1",则两个控件均匀分配剩余空间。
在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="400dp"
    android:layout_height="300dp"
    android:background="@color/teal_200"
    android:orientation="horizontal"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button1"
        android:layout_width="100dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:text="Button" />

    <Button
        android:id="@+id/button2"
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>

如果给button1加上android:layout_weight="3"button2加上android:layout_weight="1",由于剩余的空间是200,button1button2会按照3:1的比例来分配剩余空间。
在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="400dp"
    android:layout_height="300dp"
    android:background="@color/teal_200"
    android:orientation="horizontal"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button1"
        android:layout_width="100dp"
        android:layout_weight="3"
        android:layout_height="wrap_content"
        android:text="Button" />

    <Button
        android:id="@+id/button2"
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>
  • 70
    点赞
  • 289
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
课程安排 第一天 1>搭建Android开发环境 2> 创建与启动手机模拟器 3> 学习使用ANDROID操作系统 4> 开发与运行(卸载)第一个ANDROID应用 5> 项目的目录结构 6> 项目清单文件分析 7> 分析第一个ANDROID应用的启动过程 8> 电话拔打 9> 查看手机模拟器往控制台输出的日志信息 10> 如何部署应用到真实手机 11> 短信发送 12> 布局介绍 LinearLayout (线性布局)、AbsoluteLayout(绝对布局)、RelativeLayout(相对布局)、TableLayout(表格布局)、FrameLayout(帧布局) 第二天 1> 单元测试 2> 查看与输出日志信息 3> 文件操作 4> 往SDCard读写文件 5> XML解析(SAX/DOM/PULL),写xml文件 6> SharedPreferences 第三天 1> SQLite数据库添删改查操作 A.创建数据库 B.SQLiteOpenHelper自动创建数据库的原理实现 C.数据库版本变化 D.编写代码完成添删改查操作(两种实现方法) E.事务的实现 F.采用ListView实现数据列表显示 2> 采用ContentProvider对外共享数据 第四天 1> 往通信录添加联系人,和获取联系人 2> 网络--获取数据(图片、网页、xml、Json等) 3> 如何把数据通过HTTP协议提交到网络上的Web应用(get / post ) 数据大于2k的时候 A.通过Get方式提交参数给Web应用 B.通过Post方式提交参数给Web应用 C.使用HttpClient开源项目提交参数给服务器 4> 网络--通过HTTP协议实现文件上传 第五天 1> 网络--通过HTTP协议发送XML数据,并调用webservice实现手机号归属地查询 2> 网络--通过HTTP协议实现多线程断点续传下载 3> 为应用添加新的Activity与参数传递 4> 意图 第六天 1> Activity的生命周期 2> 广播接收者(实现短信监听) 3> 服务与语音刻录(实现电话监听)、使用AIDL实现进程通信 4> 音乐播放器 5> 视频播放器 第七天 1> 拍照 2> 视频录制 3> 手势识别 4> 国际化(文字、图片)、屏幕适配、样式与主题 5> 编码实现软件界面 第八天 1> 采用HTML设计软件界面 2> 传感器的使用和拖拉功能实现 3> 软件打包与发布,生成私钥签名你的软件 4> 简历介绍 第九天以后 讲解Android手机视频客户端、来电知了、新浪微博客户端等项目

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Teacher.Hu

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

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

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

打赏作者

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

抵扣说明:

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

余额充值