2.2线性布局

一、线性布局

  • 线性布局(LinearLayout)是一种比较常用且简单的布局方式。在这种布局中,所有的子元素都是按照垂直或水平的顺序排列在界面上。如果是垂直排列,每个子元素占一行,如果是水平排列,则每个子元素占一列。线性布局可以支持布局样式嵌套实现复杂的布局样式。

1、常用属性

属性含义
layout_height高度,单位:dp (wrap_content(根据内容确定高度), match_parent(填满整个界面))
layout_weight宽度,单位:dp (wrap_content, match_parent)
orietation方向(vertical,horizontal)
gravity对齐方式(left, right, center, top, bottom…)
background背景(颜色[color]、图片[drawable]、选择器[selector])
weight比重(用于瓜分手机屏幕)
padding内边距 (paddingLeft, paddingRight, paddingTop, paddingBottom)
margin外边距 (marginLeft, marginRight, marginTop, marginBottom)

二、案例(线性布局属性)

1、创建安卓应用

  • 基于Empty Activity模板创建安卓应用 - LinearLayoutDemo

2、主布局资源文件

  • 将约束布局改为线性布局,删掉默认的标签
    在这里插入图片描述
  • 添加两个按钮
<?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="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮1"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮2"/>
</LinearLayout>

3、字符串资源文件

<resources>
    <string name="app_name">线性布局演示</string>
</resources>

4、预览效果

  • 查看效果,发现两个按钮水平摆放,在窗口左上角
    在这里插入图片描述

5、设置布局属性,查看效果

(1)设置线性布局方向

  • orientation属性
<?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="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"

    tools:context=".MainActivity">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮1"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮2"/>

</LinearLayout>

在这里插入图片描述

(2)设置线性布局内边距

  • padding (paddingTop, paddingBottom, paddingLeft, paddingRight)
    在这里插入图片描述
    在这里插入图片描述

(3)设置线性布局对齐方式

  • gravity (left、center、right、top、bottom可以搭配形成很多种对齐方式)
  • 例如:居中
    在这里插入图片描述
    在这里插入图片描述
  • 还可以设置联合对齐方式(如:gravity:“center|top” :上中)
    在这里插入图片描述
    在这里插入图片描述

(4)设置线性布局背景

  • 设置背景颜色
    在这里插入图片描述
    在这里插入图片描述

  • 设置背景图片
    1、在drawable里添加图片
    在这里插入图片描述
    2、在布局资源中写入代码
    在这里插入图片描述
    3、效果
    在这里插入图片描述

(5)实现边框渐变色效果

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

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    //设置圆角
    <corners android:radius="10dp"/>
    //填充颜色
    <solid android:color="#ffaaff"/>
    //边框色、宽度
    <stroke android:width="1dp" android:color="#aa5555"/>
    <gradient android:startColor="#ffff00"
        android:centerColor="#ff3388"
        android:endColor="#ffaaff"/>
</shape>

在这里插入图片描述

三、线性布局嵌套

1、创建安卓应用

  • 基于Empty Activity创建安卓应用 - NestedLinearLayout

2、准备图片素材

  • 将三张小图片拷贝到res/drawable目录
    在这里插入图片描述

3、主布局资源文件

  • 将约束布局改为线性布局
    在这里插入图片描述
  • 添加三个线性布局,按照1:2:3比例垂直瓜分手机屏幕
    在这里插入图片描述

4、字符串资源文件

<resources>
    <string name="app_name">线性布局嵌套演示</string>
</resources>

5、查看预览效果

在这里插入图片描述

6、修改布局,查看效果

  • 在第一个布局添加一张图片和两个按钮
  • 在第二个布局里添加一个横向线性布局,里面添加四个按钮,以及在第二个布局里添加一个编辑框
  • 在第三个布局中添加一个图片
<?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="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:gravity="center"
        android:background="#ccffcc"
        android:layout_weight="1">
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="按钮1"/>
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="按钮2"/>

        </LinearLayout>
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/img01"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:gravity="center"
        android:background="#3399dd"
        android:orientation="vertical"
        android:layout_weight="2"
        android:layout_height="0dp"
        android:padding="20dp">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center">
            <Button
                android:layout_width="80dp"
                android:layout_height="wrap_content"
                android:layout_marginRight="10dp"
                android:text="按钮1"/>
            <Button
                android:layout_width="80dp"
                android:layout_height="wrap_content"
                android:layout_marginRight="10dp"
                android:text="按钮2"/>
            <Button
                android:layout_width="80dp"
                android:layout_height="wrap_content"
                android:layout_marginRight="10dp"
                android:text="按钮3"/>
            <Button
                android:layout_width="80dp"
                android:layout_height="wrap_content"
                android:text="按钮4"/>

        </LinearLayout>

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#aabbcc"
            android:lines="3"
            android:layout_marginTop="10dp"
            android:text="演示线性布局嵌套,横向和纵向的布局可以相互嵌套,形成比较复杂的局面" />

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:gravity="center"
        android:background="#ff00ff"
        android:layout_weight="3"
        android:layout_height="0dp">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/img03"/>

    </LinearLayout>

</LinearLayout>
  • 效果
    在这里插入图片描述
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android学习文档 1 一、Android前言 3 1、Android发展历程 3 2、Android发展前景 8 3、Android 应用领域 9 二、准备工作 11 1、 Android开发环境搭建 11 1.1 第一种搭建Android的开发环境步骤: 12 1.2 第二种搭建Android的开发环境步骤: 12 2、AVD设备(模拟器) 13 3、Android的模拟器Emulator 14 4、DDMS调试 14 5、Android开发需知 14 5.1 Android目录结构 14 5.2 UI控件: 15 5.3 应用程序权限 15 6、第一个android应用程序 15 7、Android组件介绍 20 三、Android应用程序架构分析 21 1、Android布局组件介绍及其实例分析 21 2.1 布局组件概述 21 2.1.1 五大布局: 21 2.1.2 属性: 21 2.1.3 常用的长度单位解析: 21 2.2 线性布局 22 2.3 表格布局 22 2.4 相对布局 23 2.5 基本界面组件 23 2、Activity生命周期及其实例分析 24 1.1 Avtivity介绍 24 1.2 Activity的生命周期 25 1.2.1 Activity的三种状态 25 1.2.2 Activity在三种状态切换时调用的方法(7种) 25 1.2.3 Activity跳转与布局管理器 27 1.2.4 Activity中与跳转相关的方法 28 1.2.5 Activity的内部切换界面 28 1.2.6 Activity的用法 28 1.3 Avtivity的回调机制 28 四、事件处理机制 29 1、事件效果 29 2、事件监听器的处理模型 30 3、监听器的事件处理模型步骤 31 4、事件监听器必须实现事件监听器接口 31 5、实现事件监听器 31 6、基于回调的事件处理 32 7、基于回调的事件传播 32 五、 Android数据存储 32 1、android的数据存储 32 2、如何在android中使用SQlite数据库? 33 2.1 使用SharedPreferences 33 2.2 练习使用SharedPreferences 33 3、要读写其他应用的SharedPreferences怎么做? 33 六、 Json数据格式解析和xml解析 34 1、JSON简介 34 2、Gson介绍 34 3、JSON与XML 34 4、Android中对xml的解析 35 4.1 学习json数据格式,编写xml文件转化成实体bean的工具类 35 1.先写一个工具类,把list集合转化成xml格式,注意支持各种类型 35 分析:用dom4j来操作 35 2.涉及的知识 35 五、开发中会用到的技术 36 1、Selector技术 36 2、Tosat显示消息提示框技术 36 3、Handler消息传递机制 36 4、Handler类的主要作用 37 六、Android实例 38 1、做多米播器的特效 38 2、做时钟的特效 39 1. 涉及的知识 40 一、完成”时钟”应用程序的操作 43 七、图形与图像处理 44

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值