Android 布局之FrameLayout


1 FrameLayout简介

对于FrameLayout,官方介绍是:
FrameLayout is designed to block out an area on the screen to display a single item. Generally, FrameLayout should be used to hold a single child view, because it can be difficult to organize child views in a way that's scalable to different screen sizes without the children overlapping each other. You can, however, add multiple children to a FrameLayout and control their position within the FrameLayout by assigning gravity to each child, using the android:layout_gravity attribute.

即,设计FrameLayout是为了显示单一项widget。通常,不建议使用FrameLayout显示多项内容;因为它们的布局很难调节。不用layout_gravity属性的话,多项内容会重叠;使用layout_gravity的话,能设置不同的位置。layout_gravity可以使用如下取值:

top
将对象放在其容器的顶部,不改变其大小.

bottom
将对象放在其容器的底部,不改变其大小.

left
将对象放在其容器的左侧,不改变其大小.

right
将对象放在其容器的右侧,不改变其大小.

center_vertical
将对象纵向居中,不改变其大小.
垂直对齐方式:垂直方向上居中对齐。

fill_vertical
必要的时候增加对象的纵向大小,以完全充满其容器.
垂直方向填充

center_horizontal
将对象横向居中,不改变其大小.
水平对齐方式:水平方向上居中对齐

fill_horizontal
必要的时候增加对象的横向大小,以完全充满其容器.
水平方向填充

center
将对象横纵居中,不改变其大小.

fill
必要的时候增加对象的横纵向大小,以完全充满其容器.

clip_vertical
附加选项,用于按照容器的边来剪切对象的顶部和/或底部的内容. 剪切基于其纵向对齐设置:顶部对齐时,剪切底部;底部对齐时剪切顶部;除此之外剪切顶部和底部.
垂直方向裁剪

clip_horizontal
附加选项,用于按照容器的边来剪切对象的左侧和/或右侧的内容. 剪切基于其横向对齐设置:左侧对齐时,剪切右侧;右侧对齐时剪切左侧;除此之外剪切左侧和右侧.
水平方向裁剪

注意: 区分“android:gravity”和“android:layout_gravity”。
android:gravity            :是对控件本身来说的,是用来设置“控件自身的内容”应该显示在“控件自身体积”的什么位置,默认值是左侧。
android:layout_gravity:是相对于控件的父元素来说的,设置该控件在它的父元素的什么位置。


2 FrameLayout示例

创建一个activity,包含2组FrameLayout:1组设置android:layout_gravity属性,另1组不设置android:layout_gravity属性。

layout文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <!-- 示例1 FrameLayout内容重叠 -->
<TextView
android:text="示例1, FrameLayout内容重叠"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<FrameLayout
android:layout_width="300dp"
android:layout_height="80dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是TextView,内容比较长"
android:background="#ff0000"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffff00"
android:text="我是按钮"/> </FrameLayout> <!-- 示例2 FrameLayout使用layout_gravity属性 -->
<TextView
android:text="示例2, 设置layout_gravity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<FrameLayout
android:layout_width="300dp"
android:layout_height="120dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="文本居左"
android:background="#ff0000"
android:gravity="center"
android:layout_gravity="left"
android:layout_margin="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="文本居中"
android:background="#ffff00"
android:gravity="center"
android:layout_gravity="center"/>
</FrameLayout>
</LinearLayout>

点击下载:源代码

运行效果:如图

Android 布局之FrameLayout的更多相关文章

  1. Android布局之FrameLayout

    框架布局(帧布局)是最简单的布局形式.所有添加到这个布局中的视图都以层叠的方式显示.第一个添加的控件被放在最底层,最后一个添加到框架布局中的视图显示在最顶层,上一层的控件会覆盖下一层的控件.这种显示方 ...

  2. Android布局及属性归总(查询用)

    常见布局 LinearLayout    线性布局        子元素任意,组织成一个单一的水平或垂直行,默认为水平方向TableLayout    表格布局        子元素为<Tabl ...

  3. android布局学习-使用FrameLayout和LinearLayout制作QQ空间底部导航栏

    [声明:本博客通过学习“J灬叶小超 ”博客而写,链接:http://www.cnblogs.com/yc-755909659/p/4288260.html] --------------------- ...

  4. android布局 FrameLayout(帧布局)详解

    看到一篇很有趣的文章对我就是冲着萌妹子看的 FrameLayout(帧布局) 前言 作为android六大布局中最为简单的布局之一,该布局直接在屏幕上开辟出了一块空白区域, 当我们往里面添加组件的时候 ...

  5. Android布局_帧布局FrameLayout

    一.FrameLayout布局概述 在这个布局中,所有的子元素都不能被指定放置的位置,他们统统放于这块区域的左上角,并且后面的子元素直接覆盖在前面的子元素之上,将前面的子元素部分和全部遮挡  如下面的 ...

  6. Android布局详解之一:FrameLayout

      原创文章,如有转载,请注明出处:http://blog.csdn.net/yihui823/article/details/6702273 FrameLayout是最简单的布局了.所有放在布局里的 ...

  7. Android(java)学习笔记93:Android布局详解之一:FrameLayout

    FrameLayout是最简单的布局了.所有放在布局里的控件,都按照层次堆叠在屏幕的左上角.后加进来的控件覆盖前面的控件. 在FrameLayout布局里,定义任何空间的位置相关的属性都毫无意义.控件 ...

  8. Android UI -- 布局介绍(布局包括FrameLayout, LinearLayout, RelativeLayout, GridLayout)

    首先介绍常用布局类 FrameLayout 最简单的布局管理器. 这个布局管理类有几个特性: 添加组件默认在左上角的. 如果添加多个组件会叠加到一起,并且都在左上角.(可以通过一gravity属性改变 ...

  9. Android中的FrameLayout帧布局

    帧布局由FrameLayout所代表,FrameLayout直接继承了ViewGoup组件. 帧布局容器为每一个增加当中的组件创建一个空白的区域(称为一个帧),每一个子组件占领一帧,这些帧都会依据gr ...

随机推荐

  1. java集合类的学习总结一

    概况总结 首先,区分最顶层接口的区别:Collection和Map的区别:前者是单个元素:后者存储的是一对元素.Collection有List和Set两个子接口,两个子接口下分别有Vector和Arr ...

  2. javascript必须懂之冒泡事件

    在学习javascript中,如果在事件的使用上出现一些反差效果,不良效果,如鼠标的移入移出时,显示你所需要的内容, 但就是没有出现,然而你不断的检查代码,逐个代码查错,还在浏览器的调试工具中调试都没 ...

  3. python find函数

    Python find() 方法检测字符串中是否包含子字符串 str ,如果指定 beg(开始) 和 end(结束) 范围,则检查是否包含在指定范围内,如果包含子字符串返回开始的索引值,否则返回-1 ...

  4. 从Windows XP系统迁移到Windows 7,Windows 8开始

    Microsoft在2014年4月8日结束了Windows XP的支持.您的公司准备好了吗?如果您还没有迁移到Windows 7或8,那就要抓紧时间了.从现在起将不再向XP系统提供安全修补程序,而仍然 ...

  5. Cocos2d-x3.x塔防游戏(保卫萝卜)从零开始(一)

    一.前提: 完成Hello Game项目的创建编译. 具体参考:Cocos2dx.3x_Hello Game项目创建篇 二.本篇目标: l  说说关于塔防游戏的想法和思路 l  实现一个简单的塔防游戏 ...

  6. layer-list实现只有左、右和下边框的圆角矩形

    项目中需要实现如下效果的布局 也就是一个左右下角带圆角,上方不带圆角的白色背景矩形,而且只有左.右和下边框,颜色为浅灰色. 当然,切一个.9图片作为背景也能实现,但是能用代码实现的还是尽量用代码实现, ...

  7. AWVS漏洞测试-01节-AWVS的主要作用

    AWVS漏洞工具简单介绍 AWVS全称: Acunetix Web Vulnerability Scanner 中文翻译就是:Acunetix网站攻击扫描器 扫描网站漏洞,通过网络爬虫Crawler的 ...

  8. asp.net 后台获取flv视频地址进行播放

    源码下载:http://download.csdn.net/detail/njxiaogui/7609687 前台:.aspx <table> <tr> <td>& ...

  9. ubuntu 休眠之后网络间接失败 can not connect to network after suspend (wake up)

    ubuntu for laptop系统在系统休眠后wakeup 之后,网络连接失败, 有线网络无法连接, 无线wifi无法连接, 只能重启后才能恢复, 此时可以采用以下方法处理: 1. 在/etc/p ...

  10. mysql性能监控工具:mycheckpoint的使用方法

    mycheckpoint 是针对mysql的一个性能监控.指标采集的python写成的工具. 工作原理说明: mycheckpoint是一段脚本,通过将其设置为crontab定时任务,每几分钟采集一次 ...