帧布局(FrameLayout)直接继承了ViewGroup组件;

帧布局容器为每一个加入其中的组件都创建了一个空白的区域,这个区域我们称之为一帧,所有每个组件都占据一帧,这些都会根据gravity属性执行自动对齐。

FrameLayout的常用XML属性及相关的方法:

XML属性 相关方法 说明
android:foreground setForeground(Drawable)  设置该帧布局容器的前景图像
android:foregroundGravity setForegroundGravity(int) 定义绘制前景图像的gracity属性

下面用一个霓虹灯效果的例子来看看帧布局的效果:

layout/main.xml

 <?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<!-- 依次定义7个TextView,先定义的TextView位于底层
后定义的TextView位于上层 -->
<TextView android:id="@+id/View01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="210px"
android:height="50px"
android:background="#ff0000"
/>
<TextView android:id="@+id/View02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="180px"
android:height="50px"
android:background="#dd0000"
/>
<TextView android:id="@+id/View03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="150px"
android:height="50px"
android:background="#bb0000"
/>
<TextView android:id="@+id/View04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="120px"
android:height="50px"
android:background="#990000"
/>
<TextView android:id="@+id/View05"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="90px"
android:height="50px"
android:background="#770000"
/>
<TextView android:id="@+id/View06"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="60px"
android:height="50px"
android:background="#550000"
/>
<TextView android:id="@+id/View07"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="30px"
android:height="50px"
android:background="#330000"
/>
</FrameLayout>

在GrapHical  Layout模式下的效果图:

上面的界面布局定义使用了FrameLayout,并向改布局添加了7个高度相同但宽度逐渐减少的TextView。

我们既然做的是霓虹灯,那就不能少了颜色。在values文件夹下面我们还定义了一个color.xml文件用来存放霓虹灯的所需的颜色[如上图所示]

Values/color.xml

 <?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="color1">#330000</color>
<color name="color2">#550000</color>
<color name="color3">#770000</color>
<color name="color4">#990000</color>
<color name="color5">#bb0000</color>
<color name="color6">#dd0000</color>
<color name="color7">#ff0000</color>
</resources>

下面我们来看看主程序,主程序使用了上面的FrameLayout布局管理器,只是程序启动了一条线程来控制周期性的改变7个TextView背景颜色,从而达到霓虹灯的效果。

com.example.framelayouttest.MainActivity.java

 package com.example.framelayouttest;

 import java.util.Timer;
import java.util.TimerTask; import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.os.Build; public class MainActivity extends ActionBarActivity { private int currentColor = 0;
//定义一个颜色数组
final int [] colors = new int[]{
R.color.color7,
R.color.color6,
R.color.color5,
R.color.color4,
R.color.color3,
R.color.color2,
R.color.color1,
}; final int [] names = new int[]{
R.id.View01,
R.id.View02,
R.id.View03,
R.id.View04,
R.id.View05,
R.id.View06,
R.id.View07,
}; TextView[] views = new TextView[7];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//首先拿到7个TextView
for (int i = 0 ; i < 7 ; i++)
{
views[i] = (TextView)findViewById(names[i]);
}
//Handler为android系统的线程通信工具,承担着主线程与分线程,分线程之间的通信功能
final Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
//表明消息来自本程序所发送;what表示什么事,when表示什么时候
if (msg.what == 0x1122) {
//依次改变7个TextView的背景色
for (int i = 0; i < 7 - currentColor ; i++) {
views[i].setBackgroundResource(colors[i+currentColor]);
}
for (int i = 7 - currentColor,j = 0 ; i < 7; i++,j++) {
views[i].setBackgroundResource(colors[j]);
}
} super.handleMessage(msg);
}
}; //定义一个线程周期性的改变currentColor变量值,第二个参数0表示无延迟,第三个参数表示延迟多久1000表示一秒
new Timer().schedule(new TimerTask() { @Override
public void run() {
currentColor ++;
if (currentColor >=6) {
currentColor=0;
}
//发送一条消息通知系统改变7个TextView组件的背景色
Message m = new Message();
//给该消息定义一个标识
m.what = 0x1122;
handler.sendMessage(m);
}
}, 0, 100);; } }

上面程序定义了一个每0.1秒执行一次任务线程,该任务改变了currentColor的值然后想handler发送了一条消息通知他更新7个textView的背景色。

才接触Android的人也许会想我一样会有点疑问:为什么不直接在run里面直接更新TextView的背景色。

我查了一下才知道Android的view和UI组件不是线程安全的,所以Android不允许开发者启动线程访问用户界面的UI组件。所以程序额外定义了一个Handler来处理TextView背景色的更新。

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

  1. Android中帧布局-FrameLayout和网格布局-GridLayout

    帧布局-FrameLayout 一.概念 帧布局中,容器为每个加入其中的空间创建一个空白的区域(成为一帧).每个空间占据一帧,这些帧会按gravity属性自动对齐. 帧布局的效果是将其中的所有空间叠加 ...

  2. .Net程序猿乐Android发展---(10)框架布局FrameLayout

    帧布局FrameLayout中全部的控件都在界面的左上側,后绘制的空间会覆盖之前的控件.布局内控件以层叠方式显示,用在游戏开发方面可能多些. 1.层叠展示                 以下这个样例 ...

  3. Android课程---帧布局 FrameLayout

    帧布局的特点是: 1.多个组件,层叠显示 2.所占位置和大小由组件决定 示例代码: <?xml version="1.0" encoding="utf-8" ...

  4. Android——布局(线性布局linearLayout,表格布局TableLayout,帧布局FrameLayout)

    线性布局: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:androi ...

  5. Android布局_帧布局FrameLayout

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

  6. Android 自学之绝对布局 AbsoluteLayout

    绝对布局(AbsoluteLayout),绝对布局就像java AWT中的空布局:所谓的绝对布局就是Android不提供任何的布局控制,而是有开发人员自己通过X坐标和Y坐标来控制组件的位置.当使用绝对 ...

  7. Android 自学之相对布局 RelativeLayout

    相对布局(RelativeLayout),相对布局容器内子组件的位置总是相对兄弟组件.父容器来决定的. RelativeLayout的XML属性及相关方法说明 XML属性 相关方法 说明 androi ...

  8. Android 自学之表格布局 TableLayout

    表格布局(TableLayout),表格布局采用行.列的形式来管理UI组件,TableLayout并不需要明确的声明多少行,多少列,而是通过TableRow.其他组件来控制表格的行数和列数. 每次想T ...

  9. Android 自学之线性布局 LinearLayout

    线性布局(LinearLayout),线性布局有点想AWT编程里面的FolwLayout,他们都会将容器里面的组件挨个的排列起来. 他们最大的区别在于:Android的线性布局不会换行:AWT里面的F ...

随机推荐

  1. 使用C#在word中插入页眉页脚

    //插入页脚 public void InsertFooter(string footer) { if (ActiveWindow.ActivePane.View.Type == WdViewType ...

  2. Linux Kernel ‘drivers/staging/wlags49_h2/wl_priv.c’本地缓冲区溢出漏洞

    漏洞名称: Linux Kernel ‘drivers/staging/wlags49_h2/wl_priv.c’本地缓冲区溢出漏洞 CNNVD编号: CNNVD-201311-068 发布时间: 2 ...

  3. [swustoj 411] 售货员的难题

    售货员的难题(0411) Time limit(ms): 5000 Memory limit(kb): 65535 Submission: 1744 Accepted: 200 Description ...

  4. ruby编程语言-学习笔记2(第4章 表达式和操作符)

    对属性和数组元素的赋值 o.m + = 1  ===>o.m = (o.m()+ 1)  # 对 o.m()这个方法的结果+1 对数组元素的赋值也是通过 方法调用来完成. 0.[] = 1 == ...

  5. MongoDB(索引及C#如何操作MongoDB)(转载)

    MongoDB(索引及C如何操作MongoDB) 索引总概况 db.test.ensureIndex({"username":1})//创建索引 db.test.ensureInd ...

  6. VMWare虚拟机USB连接问题

    错误31:连接到系统上的设备没有发挥作用 原文链接 描述 常用 VMware 虚拟机的有事应该遇到这种情况,就是装完 VMware ,启动时 VMware 下面会有个黄框中有" USB di ...

  7. Esper系列(十一)NamedWindow语法Merge、Queries、Indexing、Dropping

    On-Merge With Named Windows 功能:对window中的insert.update.delete操作进行组合运用. 格式: 1  "; 14      15  Sys ...

  8. HW1.2

    public class Solution { public static void main(String[] args) { System.out.println("Welcome to ...

  9. javascript原型和原型继承

    每一个javascript对象(null除外)都和原型对象相关联,每一个对象都从原型对象继承属性. 所有通过对象直接量创建的对象都具有同一个原型对象,并可以通过javascript代码Object.p ...

  10. [网络]让局域网的电脑通过路由的公网ip可以被访问到的设置

    http://公网IP:服务端口/访问路径(http://110.16.166.135:8081/pra/unionpay/v_asyn_notify.jspx这个地址就会通过公网ip来访问到你的ip ...