Android帧布局(Frame Layout)
Android帧布局(Frame Layout)
FrameLayout是最简单的一个布局管理器。FrameLayout为每个加入其中的组件创建一个空白区域(一帧),这些组件根据layout_gravity执行自动对齐,如果组件layout_gravity的值一样,那么后面添加的组件会覆盖之前的。如果没有指定layout_gravity,默认为固定在屏幕的左上角。下面以几个例子来说明。
1. 根据layout_gravity的值来控制组件的位置
图1
Xml代码如下:
<FrameLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.framelayout.MainActivity"> <TextView
android:id="@+id/view1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:width="20px"
android:height="20px"
android:background="#f00"/> <TextView
android:id="@+id/view2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:width="40px"
android:height="40px"
android:background="#0f0"/> <TextView
android:id="@+id/view3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|center"
android:width="60px"
android:height="60px"
android:background="#00f"/> <TextView
android:id="@+id/view4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:width="80px"
android:height="80px"
android:background="#ff0"/> <TextView
android:id="@+id/view5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center"
android:width="100px"
android:height="100px"
android:background="#f0f"/> <TextView
android:id="@+id/view6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:width="120px"
android:height="120px"
android:background="#0ff"/> <TextView
android:id="@+id/view7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:width="140px"
android:height="140px"
android:background="#123"/> </FrameLayout>
2. 霓虹灯图
图2
Xml代码如下:
<FrameLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.framelayout.MainActivity"> <TextView
android:id="@+id/view1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:width="320px"
android:height="320px"
android:background="#f00"/> <TextView
android:id="@+id/view2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:width="280px"
android:height="280px"
android:background="#0f0"/> <TextView
android:id="@+id/view3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:width="240px"
android:height="240px"
android:background="#00f"/> <TextView
android:id="@+id/view4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:width="200px"
android:height="200px"
android:background="#ff0"/> <TextView
android:id="@+id/view5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:width="160px"
android:height="160px"
android:background="#f0f"/> <TextView
android:id="@+id/view6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:width="120px"
android:height="120px"
android:background="#0ff"/> </FrameLayout>
如果只是通过上面xml代码来控制,这是静态的霓虹灯效果,可以通过代码控制实现动态的霓虹灯效果,实现原理是一个Timer定时调用TimerTask类run(),run()发送消息,然后handler从消息队列中读取出消息,并动态刷新这6个view的背景色,这样就可以实现了动态霓虹灯效果了,代码如下:
package com.example.framelayout; import java.util.Timer;
import java.util.TimerTask;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.os.Message;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView; public classMainActivity extends ActionBarActivity {
private int currentColor = 0;
protected static final String ACTIVITY_TAG="FramLayout"; final int[] colors = new int[] {R.color.color1,
R.color.color2,
R.color.color3,
R.color.color4,
R.color.color5,
R.color.color6}; final int[] names = new int[] {R.id.view1,
R.id.view2,
R.id.view3,
R.id.view4,
R.id.view5,
R.id.view6
}; TextView[]views= newTextView[names.length];
//创建一个匿名内部类,此类继承父类Handler并重写handleMessage方法。并立即创建此类的一个实例,创建后由handler指向此对象
Handlerhandler= new Handler()
{
@Override
public void handleMessage(Messagemsg)
{
if(msg.what == 0x123)
{
for(int i = 0; i < names.length; i++)
{
views[i].setBackgroundResource(colors[(i + currentColor) % names.length]);
}
currentColor++;
//Log.d(tag,message),把debug级别的日志打印到LogCat中
Log.d(ACTIVITY_TAG,"handleMessage()"+currentColor);
}
super.handleMessage(msg);//这里的super就是Handler
}
}; @Override
protected void onCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for(int i = 0; i < names.length; i++)
{
views[i] = (TextView)findViewById(names[i]);
} //1.创建一个匿名内部类,此类继承父类TimerTask并重写run方法。
//2.创建一个Timer对象并调用schedule()来启动定时器
new Timer().schedule(new TimerTask()
{
@Override
public void run()
{
handler.sendEmptyMessage(0x123);
}
},0,200); } }
实现此代码过程中,说明几点:
(1)增加color资源文件
右键values,选择New--->Android Xml file,如下图:
图3
生成color.xml文件,增加内容后如下:
<?xml version="1.0"encoding="utf-8"?>
<resources>
<color name="color1">#f00</color>
<color name="color2">#0f0</color>
<color name="color3">#00f</color>
<color name="color4">#ff0</color>
<color name="color5">#f0f</color>
<color name="color6">#0ff</color>
</resources>
(2)匿名内部类
匿名内部类适合创建那种只需要一次使用的类,创建匿名内部类时会立即创建一个该类实例,这个类定义立即消失,匿名类不能重复使用,定义匿名内部类格式如下:
new 父类构造器(实参列表)|实现接口()
{
//匿名内部类的类体部分。
}
从上面定义可看出,匿名内部类必须继承一个父类,或实现一个接口,但最多只能继承一个父类或实现一个接口。
(3)定时器启动定时器方法schedule()
timer.schedule(newMyTask(),long time1,long timer2);
第一个参数,是 TimerTask 类,在包:import java.util.TimerTask .使用者要继承该类,并实现public void run() 方法,因为 TimerTask 类实现了 Runnable 接口。
第二个参数的意思是,当你调用该方法后,该方法必然会调用 TimerTask 类 TimerTask 类中的 run()方法,这个参数就是这两者之间的差值,转换成汉语的意思就是说,用户调用 schedule() 方法后,要等待这么长的时间才可以第一次执行run() 方法。
第三个参数的意思就是,第一次调用之后,从第二次开始每隔多长的时间调用一次 run() 方法。
(4)定位类所在的包
比如Handler在哪个包呢,在elipse中选择Handler,弹出下面的内容:
图4
选着红色矩形框选择的内容就会自动在java文件多了下面的内容:
import android.os.Handler;
Android帧布局(Frame Layout)的更多相关文章
- Android线性布局(Linear Layout)
Android线性布局(Linear Layout) LinearLayout是一个view组(view group),其包含的所有子view都以一个方向排列,垂直或是水平方向.我们能够用androi ...
- Android表格布局(Table Layout)
Android表格布局(Table Layout) 先来看布局管理器之间继承关系图: 图1 可知TableLayout继承了LinearLayout,所以表格布局本质上依然是线性管理器. 表格布局采用 ...
- Android帧布局<TabHost>标签
先贴上一段代码: main.xml: <p><?xml version="1.0" encoding="utf-8"?> <Tab ...
- [android] 帧布局
/*******************2016年5月3日 更新**************************************/ 知乎:如何理解andriod中的View和framela ...
- Android View 布局流程(Layout)完全解析
前言 上一篇文章,笔者详细讲述了View三大工作流程的第一个,Measure流程,如果对测量流程还不熟悉的读者可以参考一下上一篇文章.测量流程主要是对View树进行测量,获取每一个View的测量宽高, ...
- AndroidのUI布局之layout weight
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...
- android布局 FrameLayout(帧布局)详解
看到一篇很有趣的文章对我就是冲着萌妹子看的 FrameLayout(帧布局) 前言 作为android六大布局中最为简单的布局之一,该布局直接在屏幕上开辟出了一块空白区域, 当我们往里面添加组件的时候 ...
- Android动画总结#补间动画(Tween Animation/View Animation) #帧动画(Frame Animation/Drawable Animation)#属性动画(PropertyAnimation)
1.共有三种动画,英文名字多种叫法如下 第一种动画:补间动画(Tween Animation/View Animation) 四个:RotateAnimation旋转. AlphaAnimation透 ...
- Android应用程序窗口(Activity)的测量(Measure)、布局(Layout)和绘制(Draw)过程分析
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/8372924 在前面一篇文章中,我们分析了And ...
随机推荐
- Swift基础之自定义PUSH和POP跳转动画
之前用OC代码写过PUSH和POP的转场动画,闲来无事,将其转换成Swift语言,希望对大家有帮助,转载请注明.... 如何实现PUSH和POP的转场动画? 首先,创建一个NSObject的类,分别用 ...
- VS2010每次调试都出现“此项目已经过期”提示
问题描述 最近因为项目需要,开发平台从VS2005切换成了VS2010,把一些老项目也转换到VS2010平台,因为是从低到高升级,微软还是做了很多兼容,基本上可以无缝切换,编译调试也基本正常,但是 ...
- ORACLE时间日期格式使用总结(参考网上资料汇总)
Oracle时间日期操作 sysdate+(5/24/60/60) 在系统时间基础上延迟5秒 sysdate+5/24/60 在系统时间基础上延迟5分钟 sysdate+5/24 在系统时间基础上延迟 ...
- android SlidingmMenu的入门介绍
最近了解了SlidingMenu控件的使用,之前手机qq等软件都采用了这种控件,所以还是很有必要学些这个控件的使用方法. 这个控件是基于github的一个开源项目. 地址是: https://gith ...
- 在Windows Service 2012上安装IIS 8.0 IIS 6
我的目的是在服务器上安装IIS6 ,但是受到这边文章的启发和按照他的步骤,看到了"IIS 6管理兼容性",我的问题就决解了,我这里是因为要安装vss 2005 和u8等比较早期的软 ...
- 剑指Offer——滴滴笔试题+知识点总结
剑指Offer--滴滴笔试题+知识点总结 情景回顾 时间:2016.9.18 15:00-17:00 地点:山东省网络环境智能计算技术重点实验室 事件:滴滴笔试 总体来说,滴滴笔试内容体量不算多, ...
- OpenMP并行化实例----Mandelbrot集合并行化计算
在理想情况下,编译器使用自动并行化能够管理一切事务,使用OpenMP指令的一个优点是将并行性和算法分离,阅读代码时候无需考虑并行化是如何实现的.当然for循环是可以并行化处理的天然材料,满足一些约束的 ...
- Oracle中使用游标转换数据表中指定字段内容格式(拼音转数字)
应用场景:将数据表TB_USER中字段NNDP的内容中为[sannanyinv]转换为[3男1女] 主要脚本:一个游标脚本+分割字符串函数+拼音转数字脚本 操作步骤如下: 1.创建类型 create ...
- 详解EBS接口开发之更新供应商付款方法
更新供应商地点层的付款方法API DECLARE --API 参数 l_external_payee_rec_type iby_disbursement_setup_pub.external_paye ...
- Hadoop的运行痕迹
http://www.cnblogs.com/forfuture1978/archive/2010/11/23/1884967.html 一篇讲的很好的 hadoop 基本运行环境配置信息