Android布局详解:FrameLayout[通俗易懂]

Android布局详解:FrameLayout[通俗易懂]后来也一直没有再回头看,再后来,看到评论多是负面的,也就心懒了,这个系列就没再写下去了。今天重新把文章修改一下。完全没有错不敢说,只是把当年漏写的一些内容再补进去吧。评论不删不改,大家自己看吧。我写的文章,基本都是面向新手的,所以没有很多高深的玩法(我自己也不擅长啦,我也不是高手)。所以新手看我的文章,入门即可,高深的内容不在我这里,我的庙小,装不下大神。再版修正说明:首先

大家好,又见面了,我是你们的朋友全栈君。

后来也一直没有再回头看,再后来,看到评论多是负面的,也就心懒了,这个系列就没再写下去了。

今天重新把文章修改一下。完全没有错不敢说,只是把当年漏写的一些内容再补进去吧。

评论不删不改,大家自己看吧。

我写的文章,基本都是面向新手的,所以没有很多高深的玩法(我自己也不擅长啦,我也不是高手)。

所以新手看我的文章,入门即可,高深的内容不在我这里,我的庙小,装不下大神。

再版修正说明:

首先要感谢指出我错误的朋友。前一篇修正说明,写的借口比较多,忘了道歉,态度不好,请多多包涵。

特别要感谢27楼、29楼的朋友。这篇文章的确写的不够严谨,碰到了问题就一笔带过,给读者们造成了不少误解,非常抱歉。

当然,直接回复sb的网友,我只能呵呵了。“我的庙小,装不下大神”这句话其实是送给这些朋友的。

这次重新修改了android:layout_width=”fill_parent”属性造成的android:layout_gravity失效的事情。

FrameLayout是最简单的布局了。所有放在布局里的控件,都按照层次堆叠在屏幕的左上角。后加进来的控件覆盖前面的控件。

在FrameLayout布局里,定义任何空间的位置相关的属性都毫无意义。控件自动的堆放在左上角,根本不听你的控制。

看以下的例子:

 

  1. <?xml version=“1.0” encoding=“utf-8”?>  
  2. <FrameLayout xmlns:android=“http://schemas.android.com/apk/res/android”  
  3.     android:layout_width=“fill_parent”  
  4.     android:layout_height=“fill_parent”  
  5.     >  
  6. <TextView   
  7.     android:layout_width=“fill_parent”  
  8.     android:layout_height=“wrap_content”  
  9.     android:textSize=“50sp”  
  10.     android:textColor=“#000000”  
  11.     android:text=“第一层”/>  
  12. <TextView   
  13.     android:layout_width=“fill_parent”  
  14.     android:layout_height=“wrap_content”  
  15.     android:textSize=“40sp”  
  16.     android:textColor=“#ffff00”  
  17.     android:text=“第二层”/>  
  18. <TextView   
  19.     android:layout_width=“fill_parent”  
  20.     android:layout_height=“wrap_content”  
  21.     android:textSize=“30sp”  
  22.     android:textColor=“#ff00ff”  
  23.     android:text=“第三层”/>  
  24. <TextView   
  25.     android:layout_width=“fill_parent”  
  26.     android:layout_height=“wrap_content”  
  27.     android:textSize=“20sp”  
  28.     android:textColor=“#00ffff”  
  29.     android:text=“第四层”/>  
  30. </FrameLayout>  
Android布局详解: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

 Android布局详解:FrameLayout[通俗易懂]Android布局详解:FrameLayout[通俗易懂]

变化1

我们现在来尝试改变一下他们的位置。把第一、二个文本框改成:

  1. <TextView   
  2.     android:id=“@+id/tv1”  
  3.     android:layout_width=“fill_parent”  
  4.     android:layout_height=“wrap_content”  
  5.     android:textSize=“50sp”  
  6.     android:textColor=“#000000”  
  7.     android:text=“第一层”/>  
  8. <TextView   
  9.     android:layout_width=“fill_parent”  
  10.     android:layout_height=“wrap_content”  
  11.     android:textSize=“40sp”  
  12.     android:textColor=“#ffff00”  
  13.     android:layout_toRightOf=“@id/tv1”  
  14.     android:text=“第二层”/>  
Android布局详解:FrameLayout[通俗易懂]
<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属性。把第三个文本框改成:

  1. <TextView   
  2.     android:id=“@+id/tv3”  
  3.     android:layout_width=“fill_parent”  
  4.     android:layout_height=“wrap_content”  
  5.     android:textSize=“30dip”  
  6.     android:textColor=“#ff00ff”  
  7.     android:gravity=“right”  
  8.     android:text=“第三层”/>  
Android布局详解:FrameLayout[通俗易懂]
<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

Android布局详解:FrameLayout[通俗易懂]

Android布局详解:FrameLayout[通俗易懂]

首先呢,我们不要大惊小怪。这个现象并不说明FrameLayout失效了。

gravity属性,是控制控件内部文本的格式的。而我们看我们控件的宽的属性是什么?是“fill_parent”,也就是说,我们文本框的宽度就是屏幕的宽度。那么android:gravity=”right”文本靠右,而文本框本身还是左上堆叠在一起的。不信,我们再来改改:

  1. <TextView   
  2.     android:id=“@+id/tv3”  
  3.     <strong>android:layout_width=“wrap_content”</strong>  
  4.     android:layout_height=“wrap_content”  
  5.     android:textSize=“30dip”  
  6.     android:textColor=“#ff00ff”  
  7.     android:gravity=“right”  
  8. <pre name=“code” class=“html”>      
Android布局详解:FrameLayout[通俗易懂]
<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 +

这是这篇文章被喷最多的地方。原来的总结里面,有这么一句话:
FrameLayout根本无法控制他的子控件的位置
这句话有错,子控件可以通过
android
:
layout_gravity
属性来控制自己在父控件中的位置。
废话少说,上代码

  1. <TextView   
  2.     android:id=“@+id/tv3”  
  3.     android:layout_width=”<span style=”font-family: Arial, Helvetica, sans-serif;”>fill_parent</span><span style=“font-family: Arial, Helvetica, sans-serif;”></span>  
  4.     android:layout_height=“wrap_content”  
  5.     android:textSize=“30dip”  
  6.     android:textColor=“#ff00ff”  
  7.     android:layout_gravity=“right”  
  8.     android:text=“第三层”/>  
Android布局详解:FrameLayout[通俗易懂]

<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”这个属性造成的。文本框的宽度是充满父控件,所以文字不会到右边去。

改成:

  1. <TextView   
  2.     android:id=“@+id/tv3”  
  3.     android:layout_width=“wrap_content”  
  4.     android:layout_height=“wrap_content”  
  5.     android:textSize=“30dip”  
  6.     android:textColor=“#ff00ff”  
  7.     android:layout_gravity=“right”  
  8.     android:text=“第三层”/>  
Android布局详解:FrameLayout[通俗易懂]

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

我们试了一下:

  1. <TextView   
  2.     android:layout_width=“fill_parent”  
  3.     android:layout_height=“wrap_content”  
  4.     android:textSize=“20sp”  
  5.     android:textColor=“#00ffff”  
  6.     android:layout_gravity=“center_horizontal|bottom”  
  7.     android:text=“第四层”/>  
Android布局详解:FrameLayout[通俗易懂]
<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

Android布局详解:FrameLayout[通俗易懂]

Android布局详解:FrameLayout[通俗易懂]

我用的华为手机,第四层没居中,但是跑到底下来了。也就是说 center_horizontal 没起作用。

这个错误也是因为android:layout_width=”fill_parent”造成的。改成:

  1. <TextView   
  2.     android:layout_width=“wrap_content”  
  3.     android:layout_height=“wrap_content”  
  4.     android:textSize=“20sp”  
  5.     android:textColor=“#00ffff”  
  6.     android:layout_gravity=“center_horizontal|bottom”  
  7.     android:text=“第四层”/>  
Android布局详解:FrameLayout[通俗易懂]
<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="第四层"/>

第四层就居中了。

Android布局详解:FrameLayout[通俗易懂]

总结一下,经过以上的3个实验,我们知道FrameLayout里,默认所有的控件都是左上对齐。

控件可以通过android:layout_gravity属性控制自己在父控件中的位置。

是不是有人会问,这么简单的Layout有什么用?我想还是有它存在的价值的。

当你需要自己写一个View的时候,在View里面已经完成了你的逻辑(例如游戏^_^),那么这个View只需要一个容器放置,就可以使用FrameLayout了。虽然用其他的布局也可以,但是用最简单的不是更省系统资源么。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/137065.html原文链接:https://javaforall.net

(0)
上一篇 2022年6月2日 下午4:36
下一篇 2022年6月2日 下午4:46


相关推荐

  • 领域建模与数据库建模[通俗易懂]

    首先两者比较: 我下面是引用的别人的文章,并且感觉有句话很好,不过除了这句话其它的话都不是很好,哈哈:有些人就把问题归结于Java语言本身,睡不着觉怪床歪。 我们知道:一个软件从无到有需要经过如下几个阶段:分析、设计、编程、调试、部署和运行。   编程阶段我们通常使用Java/.NET这样面向对象语言工具,可以带来很多设计上的好处,但是也存在一个奇怪的现象:很多程序员虽然在

    2022年4月16日
    38
  • sso单点登录解决方案 java_实现单点登录

    sso单点登录解决方案 java_实现单点登录单点登录让你一次性解决多应用认证的繁琐

    2022年8月16日
    11
  • https和http有什么区别(内附详细分析)

    https和http有什么区别(内附详细分析)很多站长知道https和http有所不同,但是究竟两者有什么不同浑然不知,针对这种情况,本文给大家详细分析一下https和http有什么区别。一、基本概念(http服务器–>本地浏览器,正确快速传输;https安全套接字层,http的安全版本,http+ssl层,建立一个信息安全的通道,保证数据传输的安全,确认网站的真实性)1、HTTP:超文本传输协议(HyperTextTransferProtocol)。是互联网上应用最为广泛的一种网络协议,所有的www文件都必须遵守这个…

    2022年10月16日
    4
  • IGMP协议详解_BOOTP协议

    IGMP协议详解_BOOTP协议IGMP协议详解(转载)摘要:文章来自于《TCP/IP详解》卷一第十三章。本文详细介绍IGMP协议原理及实现实例。1、引言  本文将介绍用于支持主机和路由器进行多播的Internet组管理协议(IGMP)。它让一个物理网络上的所有系统知道主机当前所在的多播组。多播路由器需要这些信息以便知道多播数据报应该向哪些接口转发。IGMP在RFC1112中定义[Deering1989].

    2025年11月18日
    4
  • 超全Python图像处理讲解(多图预警)

    超全Python图像处理讲解(多图预警)文章目录Pillow模块讲解一、Image模块1.1、打开图片和显示图片1.2、创建一个简单的图像1.3、图像混合(1)透明度混合(2)遮罩混合1.4、图像缩放(1)按像素缩放(2)按尺寸缩放1.5、图像的剪切与粘贴(1)图像粘贴(2)裁剪图像1.4、图像旋转和格式转换(1)图像旋转(2)格式转换1.5、分离和合并(1)分离(2)合并二、ImageFilter2.1、高斯模糊2.2、其它滤镜三、…

    2022年6月20日
    46
  • idea打断点调试_vs断点调试快捷键

    idea打断点调试_vs断点调试快捷键IDEA断点调试–基础篇1前言Debug用来追踪代码的运行流程。我们通常会在程序运行过程中出现异常的时候,启用Debug模式来分析定位异常发生的位置,以及在运行过程中参数的变化。通常我们也可以启用Debug模式来跟踪代码的运行流程去学习三方框架的源码。而IDEA作为我们JAVA开发最常用的工具,所以我们对于IDEA的Debug更应该去了解一下。2断点类型IDEA中对于JAVA的断点进行了分类,有如下的4类:JavaLineBreakpoints:行断

    2022年10月20日
    4

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号