首页 技术 正文
技术 2022年11月23日
0 收藏 434 点赞 4,484 浏览 4363 个字

帧布局(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模式下的效果图:

Android 自学之帧布局 FrameLayout

上面的界面布局定义使用了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背景色的更新。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,026
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,517
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,364
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,145
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,779
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,856