Android5种布局类型(1),五步搞定Android开发环境部署

本文详细介绍了Android中的线性布局、相对布局、帧布局和表格布局,包括如何调整控件尺寸、排列方向以及使用属性如match_parent、wrap_content。此外,还提到了如何运用相对布局中的定位和间距属性,以及帧布局和表格布局的特点和使用方法。
摘要由CSDN通过智能技术生成

改变btn3的layout_width的值使他变成match_parent:

在这里插入图片描述

改变三个按钮的layout_width的值,自定义为100dp、200dp、300dp:

在这里插入图片描述

同理layout_height也是可以一样根据自己的需求自己定义自己需要的高度大小。

在这里插入图片描述

之后是orientation一个关键的属性,用于控制控件的排列方向,它有两个值 vertical表示线性布局垂直显示,horizontal表示线性布局水平显示。

将orientation的值变为vertical就会变成水平显示:

在这里插入图片描述

你也可以根据需要使用嵌套线性布局即在线性布局中再使用线性布局:

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

<Button

android:id=“@+id/btn1”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:text=“A”

/>

<LinearLayout

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:orientation=“vertical”>

<Button

android:id=“@+id/btn2”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:text=“B”

/>

<Button

android:id=“@+id/btn3”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:text=“C”

/>

在这里插入图片描述

2.相对布局


相对布局是通过相对定位的方式指定控件位置,即以其它控件或父容器为参照物,摆放控件位置。

表1 设置控件间距的属性

| 控件属性 | 功能描述 |

| — | — |

| android:layout_marginTop | 设置当前控件上边界与某控件的距离 |

| android:layout_marginBottom | 设置当前控件底边界与某控件的距离 |

| android:layout_marginLeft | 设置当前控件左边界与某控件的距离 |

| android:layout_marginRight | 设置当前控件右边界与某控件的距离 |

表2 设置控件位置的属性

| 控件属性 | 功能描述 |

| — | — |

| android:layout_centerInparent | 设置当前控件位置位于父布局的中央位置 |

| android:layout_centerVertical | 设置当前控件位置位于父布局的垂直居中位置 |

| android:layout_centerHrizontal | 设置当前控件位置位于父布局的水平居中位置 |

| android:layout_above | 设置当前控件位于某控件上方 |

| android:layout_below | 设置当前控件位于某控件下方 |

| android:layout_toLeftOf | 设置当前控件位于某控件左侧 |

| android:layout_toRightOf | 设置当前控件位于某控件右侧 |

| android:layout_alignParentTop | 设置当前控件是否与父控件顶端对齐 |

| android:layout_alignParentLeft | 设置当前控件是否与父控件左对齐 |

| android:layout_alignParentRight | 设置当前控件是否与父控件右对齐 |

| android:layout_alignParentBottom | 设置当前控件是否与父控件底端对齐 |

| android:layout_alignTop | 设置当前控件的上边界与某控件的上边界对齐 |

| android:layout_alignBottom | 设置当前控件的下边界与某控件的下边界对齐 |

| android:layout_alignLeft | 设置当前控件的左边界与某控件的左边界对齐 |

| android:layout_alignRight | 设置当前控件的右边界与某控件的右边界对齐 |

表3 设置布局内边距的属性

| 布局属性 | 功能描述 |

| — | — |

| android:paddingTop | 设置布局顶部内边距的距离 |

| android:paddingBottom | 设置布局底部内边距的距离 |

| android:paddingLeft | 设置布局左边内边距的距离 |

| android:paddingRight | 设置布局右边内边距的距离 |

| android:padding | 设置布局四周内边距的距离 |

熟练的掌握以上的表,就能很好的运用相对布局了。下面通过示例来使用以上表中的内容。

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android”

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:paddingTop=“100dp”

<Button

android:id=“@+id/btn1”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:text=“A”

android:layout_alignParentTop=“true”

/>

<Button

android:id=“@+id/btn2”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:text=“B”

android:layout_centerInParent=“true”

/>

<Button

android:id=“@+id/btn3”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:text=“C”

android:layout_alignLeft=“@+id/btn2”

android:layout_marginLeft=“50dp”

android:layout_above=“@id/btn2”

/>

父布局与顶部边距为100dp,A与父控件顶端对齐,B位置位于父布局的中央位置,C的左边界与B控件的左边界对齐,C左边界与B控件的距离为50dp,C位于B控件的上方。

在这里插入图片描述

3.帧布局


帧布局是Android中最简单的一种布局,该布局为每个加入其中的控件创建一个空白区域(称为一帧,每个控件占据一帧。

表4 FrameLayout 属性

| 布局属性 | 功能描述 |

| — | — |

| android:foreground | 设置帧布局容器的前景图像(始终在所有子控件之上) |

| android:foregroundGravity | 设置前景图像显示位置 |

通过一个例子来熟悉一下帧布局:

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout xmlns:android=“http://schemas.android.com/apk/res/android”

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:foreground=“@drawable/b”

android:foregroundGravity=“center”

<Button

android:id=“@+id/btn1”

android:layout_width=“300dp”

android:layout_height=“450dp”

android:text=“A”

/>

<Button

android:id=“@+id/btn2”

android:layout_width=“150dp”

android:layout_height=“100dp”

android:text=“B”

/>

<Button

android:id=“@+id/btn3”

android:layout_width=“50dp”

android:layout_height=“50dp”

android:text=“C”

/>

从运行结果可以看出,按钮都被前景图片给挡住了,所以要注意在使用帧布局的时候,一定要记住前景图片会始终保持在最上层。

在这里插入图片描述

当我们把前景图删除后:按钮就可以显示了。

在这里插入图片描述

4.表格布局


表格布局是以表格的形式排列空间的,通过行和列将界面划分为多个单元格,每个单元格都可以添加控件。

表5 TableLayout 布局属性

| 布局属性 | 功能描述 |

| — | — |

| android:stretchColumns | 拉伸列 |

| android:shrinkColumns | 收缩列 |

| android:collapseColumns | 隐藏列 |

表6 TableLayout 控件属性

| 控件属性 | 功能描述 |

| — | — |

| android:layout_column | 设置单元格显示位置 |

| android:layout_span | 设置单元格占据几行 |

<?xml version="1.0" encoding="utf-8"?>

<TableLayout xmlns:android=“http://schemas.android.com/apk/res/android”

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:stretchColumns=“1”

<Button

android:id=“@+id/btn1”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_column=“2”

android:text=“A”

/>

<Button

android:id=“@+id/btn2”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_column=“2”

android:text=“B”

/>

<Button

android:id=“@+id/btn4”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_column=“1”

android:text=“D”

/>

<Button

android:id=“@+id/btn5”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_column=“2”
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

img

img

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

最后

一线互联网Android面试题含详解(初级到高级专题)

这些题目是今年群友去腾讯、百度、小米、乐视、美团、58、猎豹、360、新浪、搜狐等一线互联网公司面试被问到的题目。并且大多数都整理了答案,熟悉这些知识点会大大增加通过前两轮技术面试的几率

如果设置门槛,很多开发者朋友会因此错过这套高级架构资料,错过提升成为架构师的可能。这就失去了我们的初衷;让更多人都能通过高效高质量的学习,提升自己的技术和格局,升职加薪。

最后送给大家一句话,望共勉,永远不要放弃自己的梦想和追求;

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

**

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

最后

一线互联网Android面试题含详解(初级到高级专题)

这些题目是今年群友去腾讯、百度、小米、乐视、美团、58、猎豹、360、新浪、搜狐等一线互联网公司面试被问到的题目。并且大多数都整理了答案,熟悉这些知识点会大大增加通过前两轮技术面试的几率

[外链图片转存中…(img-u0seiOZK-1712372198302)]

如果设置门槛,很多开发者朋友会因此错过这套高级架构资料,错过提升成为架构师的可能。这就失去了我们的初衷;让更多人都能通过高效高质量的学习,提升自己的技术和格局,升职加薪。

最后送给大家一句话,望共勉,永远不要放弃自己的梦想和追求;

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值