首先看演示:

FrameLayout框架布局是最简单的布局形式。所有添加到这个布局中的视图都以层叠的方式显示。第一个添加的控件被放在最底层,最后一个添加到框架布局中的视图显示在最顶层,上一层的控件会覆盖下一层的控件。这种显示方式有些类似于堆栈。

当我们往里面添加组件的时候,所有的组件都会放置于这块区域的左上角;

帧布局的大小由子控件中最大的子控件决定,如果都组件都一样大的话,同一时刻就只能看到最上面的那个组件了。

1 layout_gravity

FrameLayout根本无法控制他的子控件的位置,子控件可以通过android:layout_gravity属性来控制自己在父控件中的位置,从而制定组件的对其方式。

2 layout_margin

FrameLayout布局里面的控件单独设置layout_margin类的属性没有效果。FrameLayout中的控件layout_margin设置要依赖layout_gravity属性,否则layout_margin设置无效。layout_gravity有好几个值可以设置,具体要设置哪一个呢?其实layout_gravity可以理解为设置控件的参考点,控件最终显示位置最终由layout_gravity和layout_margin共同决定。

如果想要控件正常显示,可以将控件的layout_gravity设置为top,以屏幕左上角为参考点。

3 前景图像:

永远处于帧布局最顶的,直接面对用户的图像,,就是不会被覆盖的图片

常用属性:

android:foreground:设置该帧布局容器的前景图像

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

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

3应用实例

activity代码

package mm.shandong.com.testframelayout;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout; public class TestFrameLayoutActivity extends AppCompatActivity {
FrameLayout frameLayout;
Button btn; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_frame_layout);
btn = (Button) findViewById(R.id.btn);
frameLayout = (FrameLayout) findViewById(R.id.frameLayout);
frameLayout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
int x = (int) motionEvent.getX();
int y = (int) motionEvent.getY();
int width = btn.getWidth();
int height = btn.getHeight();
btn.layout(x, y, x + width, y + height);
return false;
}
});
}
}

xml代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="下面是一个FrameLayout,默认布局" /> <FrameLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="#ff0000"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第一层"
android:textColor="#00ff00"
android:textSize="55sp" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第二层"
android:textColor="#0000ff"
android:textSize="45sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第三层"
android:textColor="#00ffff"
android:textSize="35sp" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第四层"
android:textColor="#ffff00"
android:textSize="25sp" />
</FrameLayout> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="下面是一个FrameLayout,通过layout_gravity和margin调节位置,并设置前景图片和背景颜色" /> <FrameLayout
android:layout_width="match_parent"
android:layout_height="180dp"
android:background="#ff0000"
android:foreground="@drawable/red"
android:foregroundGravity="right|bottom"> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|left"
android:layout_marginLeft="20dp"
android:layout_marginTop="0dp"
android:text="左上 left:20 Top:0"
android:textColor="#00ff00"
android:textSize="15sp" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|left"
android:layout_marginLeft="20dp"
android:layout_marginTop="25dp"
android:text="左上 left:20 Top:25"
android:textColor="#00aa00"
android:textSize="15sp" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|left"
android:layout_marginBottom="20dp"
android:layout_marginLeft="45dp"
android:text="左下 left:45 Bottom:20"
android:textColor="#0000ff"
android:textSize="15sp" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="居中"
android:textColor="#00ffff"
android:textSize="15sp" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|top"
android:layout_marginTop="30dp"
android:text="右上 top 30"
android:textColor="#ffff00"
android:textSize="15sp" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|bottom"
android:layout_marginBottom="80dp"
android:text="最上层,仍被遮盖"
android:textSize="15sp" />
</FrameLayout> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="下面是一个FrameLayout,通过layout方法设置控件具体位置,请单击framelayou" /> <FrameLayout
android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff0000"> <Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请点击别处" />
</FrameLayout>
</LinearLayout>

  

  

本人微博:honey_11

Demo下载
最后,以上例子都来源与安卓无忧,请去应用宝或者豌豆荚下载:例子源码,源码例子文档一网打尽

android FrameLayout详解的更多相关文章

  1. Android布局详解之一:FrameLayout

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

  2. Android Notification 详解(一)——基本操作

    Android Notification 详解(一)--基本操作 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 源码:AndroidDemo/Notification 文中如有纰 ...

  3. Android Notification 详解——基本操作

    Android Notification 详解 版权声明:本文为博主原创文章,未经博主允许不得转载. 前几天项目中有用到 Android 通知相关的内容,索性把 Android Notificatio ...

  4. Android ActionBar详解

    Android ActionBar详解 分类: Android2014-04-30 15:23 1094人阅读 评论(0) 收藏 举报 androidActionBar   目录(?)[+]   第4 ...

  5. Android 签名详解

    Android 签名详解 AndroidOPhoneAnt设计模式Eclipse  在Android 系统中,所有安装 到 系统的应用程序都必有一个数字证书,此数字证书用于标识应用程序的作者和在应用程 ...

  6. Android编译系统详解(一)

    ++++++++++++++++++++++++++++++++++++++++++ 本文系本站原创,欢迎转载! 转载请注明出处: http://blog.csdn.net/mr_raptor/art ...

  7. 【整理修订】Android.mk详解

    Android.mk详解 1. Android.mk 的应用范围 Android.mk文件是GNU Makefile的一小部分,它用来对Android程序进行编译. 一个Android.mk文件可以编 ...

  8. Android菜单详解(四)——使用上下文菜单ContextMenu

    之前在<Android菜单详解(二)——创建并响应选项菜单>和<Android菜单详解(三)——SubMenu和IconMenu>中详细讲解了选项菜单,子菜单和图标菜单.今天接 ...

  9. Android签名详解(debug和release)

    Android签名详解(debug和release)   1. 为什么要签名 1) 发送者的身份认证 由于开发商可能通过使用相同的Package Name来混淆替换已经安装的程序,以此保证签名不同的包 ...

随机推荐

  1. Power BI官方视频(1) Power BI Desktop 7月份更新功能概述

    2016年7月,Power BI Desktop进行了一些功能更新,提高整体的用户体验.同时也有一些新的和令人兴奋的功能.看看大概介绍,更新功能要点: 本文原文地址:Power BI官方视频(1) P ...

  2. 【原创】机器学习之PageRank算法应用与C#实现(1)算法介绍

    考虑到知识的复杂性,连续性,将本算法及应用分为3篇文章,请关注,将在本月逐步发表. 1.机器学习之PageRank算法应用与C#实现(1)算法介绍 2.机器学习之PageRank算法应用与C#实现(2 ...

  3. 生成Kindle可读的mobi和PDF电子书

    购买kindle之后,自然欣喜万分,不来自于工具本身,而来自于发现自己能够静下心来阅读长篇和复杂的文字了,可喜可贺.更重要的是,kindle减轻了我眼睛的莫大的压力.但马上就出现几个问题: 不是所有的 ...

  4. 一个简易的MysQL性能查询脚本

    如下: #!/bin/sh mysqladmin -P3306 -uroot -p ext |\ awk -F"|" \ "BEGIN{ count=0; }" ...

  5. 解决 android 高低版本 webView 里内容 自适应屏幕的终极方法

    转载请声明出处(http://www.cnblogs.com/linguanh/) 一,先说下我的情况,大家可以对号入座(嫌无聊请跳过) 我的项目要求是这样的,先从数据库里面拿出来html标签,因为加 ...

  6. 解析C#类中的构造函数

    <解析C#类中的构造函数> 一.  C#中的构造函数概述: C#中类包含数据成员和函数成员.函数成员提供了操作类中数据的某些功能,包括方法.属性.构造器和终结器.运算符和索引器. 构造函数 ...

  7. 基于Angularjs实现分页

    前言 学习任何一门语言前肯定是有业务需求来驱动你去学习它,当然ng也不例外,在学习ng前我第一个想做的demo就是基于ng实现分页,除去基本的计算思路外就是使用指令封装成一个插件,在需要分页的列表页面 ...

  8. OpenCV2:等间隔采样和局部均值的图像缩小

    图像的缩小从物理意义上来说,就是将图像的每个像素的大小缩小相应的倍数.但是,改变像素的物理尺寸显然不是那么容易的,从数字图像处理的角度来看,图像的缩小实际就是通过减少像素个数来实现的.显而易见的,减少 ...

  9. Microsoft Visual Studio 插件

    AnkhSVN BatchFormat CodeMaind Nuget Package Manager

  10. jQuery-1.9.1源码分析系列(九) CSS操作

    jquery.fn.css获取当前jQuery所匹配的元素中第一个元素的属性值[$(…).css(cssName),注意这个cssName可以是数组]或给当前jQuery所匹配的每个元素设置样式值[$ ...