Android布局详解:FrameLayout
后来也一直没有再回头看,再后来,看到评论多是负面的,也就心懒了,这个系列就没再写下去了。
今天重新把文章修改一下。完全没有错不敢说,只是把当年漏写的一些内容再补进去吧。
评论不删不改,大家自己看吧。
我写的文章,基本都是面向新手的,所以没有很多高深的玩法(我自己也不擅长啦,我也不是高手)。
所以新手看我的文章,入门即可,高深的内容不在我这里,我的庙小,装不下大神。
再版修正说明:
首先要感谢指出我错误的朋友。前一篇修正说明,写的借口比较多,忘了道歉,态度不好,请多多包涵。
特别要感谢27楼、29楼的朋友。这篇文章的确写的不够严谨,碰到了问题就一笔带过,给读者们造成了不少误解,非常抱歉。
当然,直接回复sb的网友,我只能呵呵了。“我的庙小,装不下大神”这句话其实是送给这些朋友的。
这次重新修改了android:layout_width="fill_parent"属性造成的android:layout_gravity失效的事情。
FrameLayout是最简单的布局了。所有放在布局里的控件,都按照层次堆叠在屏幕的左上角。后加进来的控件覆盖前面的控件。
在FrameLayout布局里,定义任何空间的位置相关的属性都毫无意义。控件自动的堆放在左上角,根本不听你的控制。
看以下的例子:
- <?xml version="1.0" encoding="utf-8"?>
- <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:textSize="50sp"
- android:textColor="#000000"
- android:text="第一层"/>
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:textSize="40sp"
- android:textColor="#ffff00"
- android:text="第二层"/>
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:textSize="30sp"
- android:textColor="#ff00ff"
- android:text="第三层"/>
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:textSize="20sp"
- android:textColor="#00ffff"
- android:text="第四层"/>
- </FrameLayout>
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"android:layout_height="wrap_content"android:textSize="50sp"android:textColor="#000000"android:text="第一层"/>
<TextView android:layout_width="fill_parent"android:layout_height="wrap_content"android:textSize="40sp"android:textColor="#ffff00"android:text="第二层"/>
<TextView android:layout_width="fill_parent"android:layout_height="wrap_content"android:textSize="30sp"android:textColor="#ff00ff"android:text="第三层"/>
<TextView android:layout_width="fill_parent"android:layout_height="wrap_content"android:textSize="20sp"android:textColor="#00ffff"android:text="第四层"/>
</FrameLayout>
效果如下图:layoutpic001
变化1
我们现在来尝试改变一下他们的位置。把第一、二个文本框改成:
- <TextView
- android:id="@+id/tv1"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:textSize="50sp"
- android:textColor="#000000"
- android:text="第一层"/>
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:textSize="40sp"
- android:textColor="#ffff00"
- android:layout_toRightOf="@id/tv1"
- android:text="第二层"/>
<TextView android:id="@+id/tv1"android:layout_width="fill_parent"android:layout_height="wrap_content"android:textSize="50sp"android:textColor="#000000"android:text="第一层"/>
<TextView android:layout_width="fill_parent"android:layout_height="wrap_content"android:textSize="40sp"android:textColor="#ffff00"android:layout_toRightOf="@id/tv1"android:text="第二层"/>
也就是说,让第二个文本框放在第一个文本框的右边。我们来看看效果。看到了没?还是一样的不变吧。
变化2
我们来尝试下android:gravity属性。把第三个文本框改成:
- <TextView
- android:id="@+id/tv3"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:textSize="30dip"
- android:textColor="#ff00ff"
- android:gravity="right"
- android:text="第三层"/>
<TextView android:id="@+id/tv3"android:layout_width="fill_parent"android:layout_height="wrap_content"android:textSize="30dip"android:textColor="#ff00ff"android:gravity="right"android:text="第三层"/>
看看效果如何?天哪!竟然没有覆盖,而是错开了!!!
layoutpic002
首先呢,我们不要大惊小怪。这个现象并不说明FrameLayout失效了。
gravity属性,是控制控件内部文本的格式的。而我们看我们控件的宽的属性是什么?是“fill_parent”,也就是说,我们文本框的宽度就是屏幕的宽度。那么android:gravity="right"文本靠右,而文本框本身还是左上堆叠在一起的。不信,我们再来改改:
- <TextView
- android:id="@+id/tv3"
- <strong>android:layout_width="wrap_content"</strong>
- android:layout_height="wrap_content"
- android:textSize="30dip"
- android:textColor="#ff00ff"
- android:gravity="right"
- <pre name="code" class="html">
<TextView android:id="@+id/tv3"<strong>android:layout_width="wrap_content"</strong>android:layout_height="wrap_content"android:textSize="30dip"android:textColor="#ff00ff"android:gravity="right"
<pre name="code" class="html">
android:text="第三层"/>
我们让第三个文本框的宽度自适应,也就是保证显示全文字即可。这个时候看一下效果呢?是不是打回原形啦?哈哈哈。
变化2 +
- <TextView
- android:id="@+id/tv3"
- android:layout_width="<span style="font-family: Arial, Helvetica, sans-serif;">fill_parent</span><span style="font-family: Arial, Helvetica, sans-serif;">"</span>
- android:layout_height="wrap_content"
- android:textSize="30dip"
- android:textColor="#ff00ff"
- android:layout_gravity="right"
- android:text="第三层"/>
<TextView android:id="@+id/tv3"android:layout_width="<span style="font-family: Arial, Helvetica, sans-serif;">fill_parent</span><span style="font-family: Arial, Helvetica, sans-serif;">"</span>android:layout_height="wrap_content"android:textSize="30dip"android:textColor="#ff00ff"android:layout_gravity="right"android:text="第三层"/>
效果和layoutpic001图一样。看上去貌似android:layout_gravity="right"这句话没有起作用。其实是因为android:layout_width="fill_parent"这个属性造成的。文本框的宽度是充满父控件,所以文字不会到右边去。
改成:
[html] view plaincopy
print?
- <TextView
- android:id="@+id/tv3"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textSize="30dip"
- android:textColor="#ff00ff"
- android:layout_gravity="right"
- android:text="第三层"/>
<TextView android:id="@+id/tv3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="30dip"android:textColor="#ff00ff"android:layout_gravity="right"android:text="第三层"/>
效果和layoutpic002图一样。android:layout_gravity="right"这句话就起作用了。
变化3
有回帖说用:android:layout_gravity="center_horizontal|bottom"
我们试了一下:
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:textSize="20sp"
- android:textColor="#00ffff"
- android:layout_gravity="center_horizontal|bottom"
- android:text="第四层"/>
<TextView android:layout_width="fill_parent"android:layout_height="wrap_content"android:textSize="20sp"android:textColor="#00ffff"android:layout_gravity="center_horizontal|bottom"android:text="第四层"/>
效果如何?如下图
layoutpic003
我用的华为手机,第四层没居中,但是跑到底下来了。也就是说 center_horizontal 没起作用。
这个错误也是因为android:layout_width="fill_parent"造成的。改成:
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textSize="20sp"
- android:textColor="#00ffff"
- android:layout_gravity="center_horizontal|bottom"
- android:text="第四层"/>
<TextView android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="20sp"android:textColor="#00ffff"android:layout_gravity="center_horizontal|bottom"android:text="第四层"/>
第四层就居中了。
总结一下,经过以上的3个实验,我们知道FrameLayout里,默认所有的控件都是左上对齐。
控件可以通过android:layout_gravity属性控制自己在父控件中的位置。
是不是有人会问,这么简单的Layout有什么用?我想还是有它存在的价值的。
当你需要自己写一个View的时候,在View里面已经完成了你的逻辑(例如游戏^_^),那么这个View只需要一个容器放置,就可以使用FrameLayout了。虽然用其他的布局也可以,但是用最简单的不是更省系统资源么。
Android布局详解:FrameLayout相关推荐
- Android - 布局详解之LinearLayout与RelativeLayout
本篇博文只针对LinearLayout与RelativeLayout 我们在新建一个布局文件时,一般遵循这样的思路:先确定该文件对应的界面中各个布局和控件的位置和大小,然后再来设置各个布局和控件的其他 ...
- qt布局嵌套_QDockWidget嵌套布局详解-实现Visual Studio布局
概述 许多工程软件,如Qt Creator,VS,matlab等,都是使用dock布局窗口,这样用户可以自定义界面,自由组合窗口. Qt的嵌套布局由QDockWidget完成,用Qt Creator拖 ...
- android rotate 动画,Android RotateAnimation详解
RotateAnimation旋转坐标系为以旋转点为坐标系(0,0)点.x轴为0度,顺时针方向旋转一定的角度. 1.RotateAnimation(fromDegrees, toDegrees) [默 ...
- android中帧布局效果,布局之FrameLayout(帧布局)详解
New UI-布局之FrameLayout(帧布局)详解 --转载请注明出处:coder-pig,欢迎转载,请勿用于商业用途!本节引言:FrameLayout(帧布局)可以说是六大布局中最为简单的一 ...
- Android UI详解之布局管理器(一)
Android UI详解之布局管理器 一.布局管理器 ①顶级父类View ②子类GroupView ③AbsoluteLayout.FrameLayout.LinearLayout.GridLayou ...
- Android开发重点难点1:RelativeLayout(相对布局)详解
前言 啦啦啦~博主又推出了一个新的系列啦~ 之前的Android开发系列主要以完成实验的过程为主,经常会综合许多知识来写,所以难免会有知识点的交杂,给人一种混乱的感觉. 所以博主推出"重点难 ...
- android fragment 优势,Android Fragment详解
参考网址:Android Fragment详解 一.什么是Fragment? Fragment:是Android3.0开始新增的概念,意为碎片.Fragment是依赖于Activity的,不能独立存在 ...
- 【转】Android菜单详解——理解android中的Menu--不错
原文网址:http://www.cnblogs.com/qingblog/archive/2012/06/08/2541709.html 前言 今天看了pro android 3中menu这一章,对A ...
- Android菜单详解——理解android中的Menu
前言 今天看了pro android 3中menu这一章,对Android的整个menu体系有了进一步的了解,故整理下笔记与大家分享. PS:强烈推荐<Pro Android 3>,是我至 ...
- Android LayoutInflater详解
Android LayoutInflater详解 在实际开发中LayoutInflater这个类还是非常有用的,它的作用类 似于findViewById().不同点是LayoutInflater是用来 ...
最新文章
- ISP采用的 ISIS链接状态路由协议
- BeanFactory和ApplicationContext有什么区别
- 成功男士的健康心理特征
- 关于复旦大学英语水平测试和计算机应用能力水平测试成绩记载事宜的说明,复旦大学水平测试(FCT、FET)近期通知概览 | 一周复旦热问...
- 概要设计说明书任务分配
- 报错ClassNotFoundException:org.springframework.cloud.client.loadbalancer.LoadBalancerClientsProperties
- Unity3D开发体验
- FER 人脸情绪识别系统
- 逆水寒服务器维护26号,逆水寒维护到几点 逆水寒8.23维护公告时间
- Apex英雄手游国服何时上线 Apex英雄手游和端游互通吗
- Smoke Loader、AZORult***通过虚假海啸警报传播到了日本
- 地理坐标xy表示什么_地理坐标怎么写 书写格式及方法
- geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?
- 小程序一键生成系统网站源码
- 为什么机油使用后变红_汽车机油用完后放出油红色怎么回事
- 运维之思科篇 -----3.HSRP(热备份路由协议),STP(生成树协议),PVST(增强版PST)
- K8s(Kubernetes)架构笔记
- 像素深度是什么?在arcgis中如何提取像素值以及经纬度(或x、y坐标)?
- 前端工程师简历怎么写?
- 质检总局公布打假案例:天猫、京东卖伪劣产品
热门文章
- python 美化输出_如何让Python输出更漂亮:PrettyPrinter美化打印包使用手册
- (附源码)Springboot校园万能跑系统 毕业设计 160934
- 基于分析Laplace方程“放射状”函数特解的基本解引入
- SpringBoot整合CXF框架实现Webservice服务端
- 常见的弱口令字典1000~一石三鸟
- 各种redhat版本下载
- 信号与系统实验一:基本信号的产生和实现
- 计算机网络强制100M,网络连通性故障分析与排除二例
- 红帽linux系统安装指导
- 《TCP/IP详解 卷1:协议》PDF分享