安卓app开发概念系列「1」--view与viewgroup
ccwgpt 2024-11-07 09:55 64 浏览 0 评论
安卓app开发中会遇到各种概念,如果不能很好理解这些概念,入门会受到各种坎坎。下面跟大家一起探讨安卓开发中的一些概念,欢迎各路大神无私评论。本文资料大部分来自网络,把一些经典的东西整理成文,以便日后参考。
一 布局系列中的概念 布局包含view控件和容纳控件的容器盒子viewgroup
1 view与viewgroup
View是一种界面层的控件的一种抽象,它代表了一个控件,是Android中所有控件的基类。简单点就是界面中可以看到的各种可视东东 button label text talbe 等等都 是view,大的小的,各种颜色的,各种形状的。每种控件又有自己的属性以及对事件的响应能力。
2. viewgroup是各种布局的盒子,其中的各种布局又可视为容纳各种view控件的子盒子。也就是说常见的布局 LinearLayout(线性布局)、TableLayout(表格布局)、RelativeLayout(相对布局)、FrameLayout(层布局)、AbsoluteLayout(绝对布局)、GridLayout(网格布局)、ConstraintLayout(约束布局)都是viewgroup的孩子。每一个ViewGroup都可以嵌套其他的ViewGroup和View(视图)。一个ViewGroup的大小是相对的,它即可以是其他ViewGroup的父容器,也可以是其他ViewGroup的子容器。。
3.view与viewgroup关系以及viewgroup的布局继承关系
下面的view继承关系体现了view和viewgroup的关系。
【注】以下内容来自 https://www.cnblogs.com/thomson-fred/p/10117352.html
(1)线性布局
线性布局会将容器内的所有控件一个挨着一个地排列。
属性:
1. 排列方向
android:orienation = “ horizontal/vertical”
水平排列和垂直排列,Android中默认为垂直排列vertical
注意:默认情况下水平和垂直方向的排列只占一行,如果用android:layout_width来设定控件的宽度,如果控件宽度太大,超出屏幕的显示范围,屏幕是不会显示超出的范围的。
2. 对齐方式
用于控制元素(例如文字)在该控件里的显示位置。
属性值:
可选的值有:top、bottom、left、right、center_vertical、fill_vertical、center_horizontal、fill_horizontal、center、fill、clip_vertical
也可以同时使用两个属性值,中间用 | 竖线隔开
例如:
android:gravity = “buttom|center_horizontal”
如果定义在控件中的底部,垂直居中
举一个简单例子,我用LinearLayout线性布局来实现常用的计算器
前面四行都很容易理解,用一个Linearout来包裹4个button控件,并且设定排列方向为水平方向。这里只列出其中一行的布局代码,其他三行的代码与第一行几乎相同。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="mc" android:layout_weight="1">
//layout_weight设定水平布局中控件的占比
</Button>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="m+"
android:layout_weight="1">
</Button>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="m-"
android:layout_weight="1">
</Button>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="mr"
android:layout_weight="1">
</Button>
</LinearLayout>
最关键的是下面两行,即用绿色框框住的那一部分控件如何布局。这里我使用布局控制器内部嵌套布局控制器的方法。首先将绿色框内部的控件分成三个层级(我分别用不同颜色标注出来了)。第一个层级是绿色框,包含两个两列,即两个红色框。第二个层级是红色框,每个红色框看成一个整体的列,第一个列是左边的红色框,其内部包含两个行;第二个列是右边的红色框,即“=”号,包含一个垂直布局的列,占位两行。再对左边的红色框进行第三层级的拆分。可以拆分成两行,每一行占3个位。
于是就有下面的代码:
<!--绿色框-->
<LinearLayout android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!--红色框-->
<LinearLayout android:orientation="vertical"
android:layout_weight="3"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<!--蓝色框-->
<LinearLayout android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="1"></Button>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2"></Button>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="3"></Button>
</LinearLayout>
<!--蓝色框 -->
<LinearLayout android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="0">
</Button>
<Button
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=".">
</Button>
</LinearLayout>
</LinearLayout>
<!--红色框,=号-->
<LinearLayout android:orientation="vertical"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="=">
</Button>
</LinearLayout>
</LinearLayout>
(2)相对布局
相对布局是最灵活的一种布局方式,可以相对父容器和相对与其他控件进行布局。
主要参数有:
1. 是否对齐父容器的语法格式为true/false:
例如:android:layout_alignParentLeft = “true”
2. 为于给定ID控件不同方位的语法格式:
例如:android:layout_above="@id/btn1"
@id/btn1 控件的ID必须是事前已经定义好的
android:layout_alignParentLeft 该控件是否对齐父容器的左端
android:layout_alignParentRight 该控件是否齐其父容器的右端
android:layout_alignParentTop 该控件是否对齐父容器的顶部
android:layout_alignParentBottom 该控件是否对齐父容器的底部
android:layout_centerInParent 该控件是否相对于父容器居中
android:layout_toLeftOf 该控件位于给定ID控件的左方
android:layout_toRightOf 该控件位于给定ID控件的右方
android:layout_above 该控件位于给定ID控件的上方
android:layout_below 该控件位于给定ID控件的下方
android:layout_centerHorizontal 该控件是否横向居中
android:layout_centerVertical 该控件是否垂直居中
实例代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/darkslategray" >
<Button android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true"
android:text="下">
</Button>
<Button android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/btn1"
android:layout_above="@id/btn1"
android:text="左">
</Button>
<Button android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/btn1"
android:layout_above="@id/btn1"
android:text="右">
</Button>
<Button android:id="@+id/btn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/btn2"
android:layout_above="@id/btn2"
android:background="@color/white"
android:text="上">
</Button>
</RelativeLayout>
效果图
(3)表格布局
表格布局是最规整的一种布局方式。想象一下EXCEL表格规整的行和列,android布局管理器中的TableLayout与EXCEL中的单元格有不少相似之处。如果学过HTML,用过tbale,tr,td标签,应该也会对下面的用法有更好的理解。
表格布局由一个TableLayout包裹起来,内部是一行行的控件,每一行用一个TableRow来包裹,每一行的元素个数(即列数)都是可以不同的。默认情况下,一行中的所有控件是等宽的。
控件属性,在TableLayout中定义,对所有行起作用:
1. android:collapseColumns=”” #指定被隐藏的列的序号
2. android:shrinkColumns=”” #指定被收缩的列的列序号
3. android:stretchColumns=”” #指定被拉伸的列的列序号
4. android:layout_column="3": #表示跳过第三个控件,直接显示下一个控件,注意:这个属性从1开始计数
5. android:layout_span="3" #表示合并3个单元格,也就说这个组件占3个单元格
注意:上面属性的使用场合,是在TableLayout还是在TableRow中使用;如果是在TableRow中使用,需要在TableRow的子控件中添加属性。前四个属性都是添加在TableLayout中的,最是添加在TableRow的子控件中。
实例:
就以我们平常用的日历为案例(由于屏幕太小,放不下最后一列星期六的日期)
实例代码:
由于以下代码有很多相似之处,我只截取了比较重要的一部分
1. 总体框架
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/TableLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TableRow>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="周日"
android:background="#00000000"/> #去除button控件的样式
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="周一"
android:background="#00000000"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="周二"
android:background="#00000000"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="周三"
android:background="#00000000" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="周四"
android:background="#00000000"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="周五"
android:background="#00000000" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="周六"
android:background="#00000000" />
</TableRow>
<TableRow>
... ...
</TableRow>
... ...
... ...
</TableLayout>
2. 最后一行“不显示日期”的合并单元格样式
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="30" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_span="2"
android:background="#ffffff"
android:text="不显示日期" />
(4)绝对布局
绝对布局指定每个控件在手机上的具体坐标,每个控件的位置和大小是固定的。由于不同手机屏幕大小可能不同,所以绝对布局只适用于固定的手机屏幕。平常用得比较少,这里就不做详细介绍了。
(5)层布局
层布局也叫帧布局。每个控件占据一层,后面添加的层会覆盖前面的层。如果后面的层的大小大于或者等于前面的层,那么前面的层就会被完全覆盖。后面层中的控件就看不到了。实际应用中如果想要得到一个空间浮现在另一个控件的上方,可以在控件内部嵌套层布局。
下面是层布局的原理图:
实例代码:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/FrameLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:foregroundGravity="right|bottom">
<TextView
android:layout_width="200dp"
android:layout_height="200dp"
android:background="#FF6143" /> //橙色
<TextView
android:layout_width="150dp"
android:layout_height="150dp"
android:background="#7BFE00" /> //绿色
<TextView
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#FFFF00" /> //黄色
</FrameLayout>
效果图,每一个textview都会被前一个textview覆盖:
实际应用:
在手机程序设计中,绝对布局基本上不用,用得相对较多的是线性布局和相对布局。
相关推荐
- 用Deepseek扩写土木工程毕业论文实操指南
-
用Deepseek扩写毕业论文实操指南一、前期准备整理现有论文初稿/提纲列清楚论文核心框架(背景、现状、意义、方法、数据、结论等)梳理好关键文献,明确核心技术路线二、Deepseek扩写核心思路...
- 985学霸亲授,DeepSeek也能绘6大科研图表,5分钟就出图
-
在实验数据处理中,高效可视化是每个科研人的必修课。传统绘图软件操作复杂、耗时费力,而智能工具DeepSeek的出现彻底改变了这一现状。本文将详解如何用DeepSeek一键生成六大科研常用图表,从思维导...
- AI写论文刷屏?大学生正在丢掉的思考力
-
一、宿舍深夜:当论文变成"Ctrl+C+V"凌晨两点的大学宿舍,小王对着电脑屏幕叹气。本该三天前开始写的近代史论文,此刻还一片空白。他熟练打开某AI写作网站,输入"论五四运动的...
- Grok在辅助论文写作上能不能既“聪明”又“可怕”?!
-
AcademicIdeas-学境思源AI初稿写作随着人工智能技术的飞速发展,论文写作这一学术任务正迎来新的助力。2025年2月18日,美国xAI公司推出了备受瞩目的Grok3模型,其创始人埃隆·...
- 大四论文沟通场景!音频转文字难题听脑AI来化解
-
大四学生都知道,写论文时和导师沟通修改意见,简直是“过关斩将”。电话、语音沟通完,想把导师说的修改方向、重点要求记下来,麻烦事儿可不少。手写记不全,用普通录音转文字工具,转完还得自己慢慢找重点,稍不注...
- 论文写作 | 技术路线图怎么画?(提供经典优秀模板参考)
-
技术路线图是一种图表或文字说明,用于描述研究目标、方法和实施计划。它展示了研究的整体框架和步骤,有助于读者理解研究的逻辑和进展。在课题及论文中,技术路线图是常见的一部分,甚至是一个类似心脏一样的中枢器...
- 25年信息系统项目管理师考试第2批论文题目写作建议思路框架
-
25年信息系统项目管理师考试第2批论文题目写作建议思路框架--马军老师
- 微信购物应尽快纳入法律框架(微信购物管辖)
-
符向军近日,甘肃省工商行政管理局发布《2016年上半年信息分析报告》。报告显示,微信网购纠纷迅猛增长,网络购物投诉呈上升趋势。投诉的主要问题有出售的商品质量不过关、消费者通过微信付款后对方不发货、购买...
- 泛珠三角区域网络媒体与腾讯微信签署《战略合作框架协议》
-
新海南客户端、南海网7月14日消息(记者任桐)7月14日上午,参加第四届泛珠三角区域合作网络媒体论坛的区域网络媒体负责人及嘉宾一行到腾讯微信总部座谈交流,并签署《战略合作框架协议》(以下简称《框架协...
- 离线使用、植入微信-看乐心Mambo手环如何打破框架
-
从2014年开始智能手环就成功进入人们的生活,至今已经演变出数据监测、信息推送、心率监测等诸多五花八门的功能,人们选择智能手环并不指望其能够改变身体健康情况,更多的是通过数据来正视自身运动情况和身体健...
- 华专网络:如何零基础制作一个网站出来?
-
#如何零基础制作一个网站出来?#你是不是觉得网站建设很复杂,觉得自己是小白,需求不明确、流程搞不懂、怕被外包公司坑……这些问题我都懂!今天华专网络就用大白话给你捋清楚建站的全流程,让你轻松get网站制...
- WAIC2024丨明日上午9点,不见不散!共同探讨智能社会与全球治理框架
-
大咖云集,硕果闪耀WAIC2024世界人工智能大会智能社会论坛将于7月5日9:00-12:00与你相约直播间WAIC2024上海杨浦同济大学哔哩哔哩多平台同步直播探讨智能社会与全球治理框架WAIC...
- 约基奇:森林狼换来戈贝尔时大家都在嘲笑 他们的阵容框架很不错
-
直播吧5月4日讯西部季后赛半决赛,掘金将迎战森林狼,约基奇赛前接受采访。约基奇说道:“当蒂姆-康纳利(森林狼总经理、前掘金总经理&曾选中约基奇)做了那笔交易(换来戈贝尔)时,每个人都在嘲笑他...
- 视频号带货为什么一个流量都没有?顶级分析框架送给你
-
视频号带货为什么一个流量都没有?遇到问题,一定是步步来分析内容,视频号带货一个流量都没有,用另外一个意思来讲,就可以说是零播放。为什么视频号带货一个流量都没有?跟你说再多,都不如来个分析框架。1、是否...
你 发表评论:
欢迎- 一周热门
- 最近发表
- 标签列表
-
- MVC框架 (46)
- spring框架 (46)
- 框架图 (58)
- flask框架 (53)
- quartz框架 (51)
- abp框架 (47)
- jpa框架 (47)
- laravel框架 (46)
- springmvc框架 (49)
- 分布式事务框架 (65)
- scrapy框架 (56)
- shiro框架 (61)
- 定时任务框架 (56)
- java日志框架 (61)
- JAVA集合框架 (47)
- grpc框架 (55)
- ppt框架 (48)
- 内联框架 (52)
- winform框架 (46)
- gui框架 (44)
- cad怎么画框架 (58)
- ps怎么画框架 (47)
- ssm框架实现登录注册 (49)
- oracle字符串长度 (48)
- oracle提交事务 (47)