首先看演示:

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. android表白app

    一.前言 马上就要520和521了,是不是还有像我一样的单身狗啊.我就知道有,所以这两天简单写了这个小程序(其实是替别人写的),虽然我并不会用去骗女孩子(因为最近太忙了,实习完之后要搞毕设,要搞论文啊 ...

  2. 小菜学习设计模式(四)—原型(Prototype)模式

    前言 设计模式目录: 小菜学习设计模式(一)—模板方法(Template)模式 小菜学习设计模式(二)—单例(Singleton)模式 小菜学习设计模式(三)—工厂方法(Factory Method) ...

  3. ng1和ng2的部分对比----angular2系列(四)

    前言: angular2相比angular1做了革命性的改变.对于开发者来说,我们知道它框架的实现上改变极大.我们看到代码也能发现它写法上变化很大,似乎完全是另一个东西. 但是当我们真正去写下去的时候 ...

  4. 使用RMAN创建复制数据库

    我的实验环境: - 源数据库A机: RHEL6.4 + Oracle 11.2.0.4 IP地址:192.168.99.159 db_name=oradb 数据库已正常运行 - 复制数据库B机: RH ...

  5. Myeclipse中导入项目后java类中汉字注释出现乱码问题(已解决)

    今天重装系统,安装了新的Myeclipse后,导入之前的项目后,,出现了乱码问题.乱码问题主要是java类中的注释,而jsp页面中汉字却完好如初: 右键项目,查看项目的编码格式,UTF-8,把java ...

  6. 一种C#生成符合Java规则的二进制文件方法

    文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/. 1.一个项目中的真实问题 实际项目中,本想通过C#制作小工具生成SHP ...

  7. jquery兼容实验

    使用jquery时候,曾经遇到两个问题: 1.一个页面里,使用了很多基于jquery的前端ui库,但是各个库之间对jquery的依赖的版本不一样,有的依赖jquery1.4,jquery2.0用不了: ...

  8. HTTP首部

    前面有几篇博文介绍了HTTP协议.HTTP请求方法详解.Javascript中Cookie的那些事儿.HTTPS,今天我们来聊一聊关于HTTP首部的那些事儿 HTTP协议的请求和响应报文中肯定包含HT ...

  9. Win 10 UWP开发系列:设置AppBarButton的图标

    在WP8以前,页面最下面的四个小圆按钮是不支持绑定的,WP8.1 RT之后,系统按钮升级成了AppBarButton,并且支持绑定了.在Win10 UWP开发中,按钮的样式发生了变化,外面的圆圈没有了 ...

  10. ASP.NET MVC对WebAPI接口操作(添加,更新和删除)

    昨天<怎样操作WebAPI接口(显示数据)>http://www.cnblogs.com/insus/p/5670401.html 既有使用jQuery,也有使作HttpClient来从数 ...