Android 帧布局FrameLayout之霓虹灯效果

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

XML 属性      相关属性说明
android:foregroundsetForeground(Drawable)设置该帧布局容器的前景图像
android:foregroundGravitysetForegroundGravity(int)定义绘制前景图像的gravity属性

帧布局的页面定义文件:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     >
 6     <TextView
 7         android:id = "@+id/vieww01"
 8         android:layout_width= "wrap_content"
 9         android:layout_height= "wrap_content"
10         android:layout_gravity = "center"
11         android:width="200pt"
12         android:height="200pt"
13         android:background="#f00" />
14     <TextView
15         android:id="@+id/vieww02"
16         android:layout_width= "wrap_content"
17         android:layout_height= "wrap_content"
18         android:layout_gravity = "center"
19         android:width = "160pt"
20         android:height = "160pt"
21         android:background="#0f0"/>
22     <TextView
23         android:id = "@+id/vieww03"
24         android:layout_width = "wrap_content"
25         android:layout_height = "wrap_content"
26         android:layout_gravity = "center"
27         android:width="110pt"
28         android:height="110pt"
29         android:background="#00f" />
30     <TextView
31         android:id = "@+id/vieww04"
32         android:layout_width="wrap_content"
33         android:layout_height="wrap_content"
34         android:layout_gravity = "center"
35         android:width="80pt"
36         android:height="80pt"
37         android:background="#ff0"
38         />
39     <TextView
40         android:id = "@+id/vieww05"
41         android:layout_width="wrap_content"
42         android:layout_height="wrap_content"
43         android:layout_gravity = "center"
44         android:width="50pt"
45         android:height="50pt"
46         android:background="#f0f"
47         />
48     <TextView
49         android:id = "@+id/vieww06"
50         android:layout_width="wrap_content"
51         android:layout_height="wrap_content"
52         android:layout_gravity = "center"
53         android:width="20pt"
54         android:height="20pt"
55         android:background="#0ff"
56         />
57 
58 </FrameLayout>

主程序代码:

 1 package com.example.think.myapplication;
 2 
 3         import android.os.Handler;
 4         import android.os.Message;
 5         import android.os.Bundle;
 6         import android.widget.TextView;
 7         import android.app.Activity;
 8         import java.util.Timer;
 9         import java.util.TimerTask;
10 
11 public class MainActivity extends Activity {
12 
13     private int currentColor = 0;
14 //  定义一个颜色数组
15     final int[] colors = new int[]{
16             R.color.color1,
17             R.color.color2,
18             R.color.color3,
19             R.color.color4,
20             R.color.color5,
21             R.color.color6
22     };
23 
24     final int[] names = new int[]{
25             R.id.vieww01,
26             R.id.vieww02,
27             R.id.vieww03,
28             R.id.vieww04,
29             R.id.vieww05,
30             R.id.vieww06
31     };
32     TextView [] views = new TextView[names.length];
33     Handler handler = new Handler(){
34         @Override
35         public void handleMessage(Message msg){
36             //表明消息来自本程序所发送的
37             if (msg.what == 0x123){
38                 for (int i = 0;i < names.length; i++){
39                     views[i].setBackgroundResource(colors[(i+currentColor) % names.length]);
40                 }
41                 currentColor++;
42             }
43             super.handleMessage(msg);
44         }
45     };
46     @Override
47     protected void onCreate(Bundle savedInstanceState) {
48         super.onCreate(savedInstanceState);
49         setContentView(R.layout.activity_main);
50         for (int i = 0; i < names.length ; i++){
51             views[i] = (TextView) findViewById(names[i]);
52         }
53         //定义一个线程周期性地改变currentColor变量值
54         new Timer().schedule(new TimerTask() {
55             @Override
56             public void run() {
57                 //发送一条空消息通知系统改变6个TextView组件的背景色
58                 handler.sendEmptyMessage(0x123);
59             }
60         },0,200);
61     }
62 }

/res/values目录下新建colors.xml文件,配置如下:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <resources>
 3     <color name="colorPrimary">#3F51B5</color>
 4     <color name="colorPrimaryDark">#303F9F</color>
 5     <color name="colorAccent">#FF4081</color>
 6     <color name="color1">#f00</color>
 7     <color name="color2">#0f0</color>
 8     <color name="color3">#00f</color>
 9     <color name="color4">#ff0</color>
10     <color name="color5">#f0f</color>
11     <color name="color6">#0ff</color>
12 </resources>

 

转载于:https://www.cnblogs.com/GavinYin/p/5958639.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
<h3>回答1:</h3><br/>Android中的FrameLayout布局是一种简单的布局方式,它允许在同一屏幕上放置多个控件,这些控件可以重叠在一起。FrameLayout布局的特点是:控件可以放置在任何位置,但是它们的大小和位置都是相对于FrameLayout的大小和位置而言的。在FrameLayout中,后添加的控件会覆盖先添加的控件。因此,FrameLayout常用于实现一些简单的界面效果,如图片轮播、广告展示等。 <h3>回答2:</h3><br/>Android中的Framelayout布局是从根据子视图的层次结构绘制学习图像布局。与其他布局不同,它允许多个子视图重叠在同一个位置上。Framelayout布局通常用于包含多个视图的复杂用户界面,其中每个视图叠加到下一个视图上。一个常见的例子是使用Framelayout布局创建引导屏幕和体验界面。 Framelayout布局还可以在屏幕的不同部分显示不同的内容。例如,如果你想在一个页面上显示一个照片和一个文本框,你可以使用Framelayout布局将照片放在顶部,然后将文本框放在底部,从而将两者分别放在屏幕的顶部和底部。 Framelayout布局非常适合在Android应用中使用,因为它允许不同的视图叠加在一起,从而创建优美且动态的用户界面。它也非常灵活,可以与其他布局组合使用,例如linearlayout和relativelayout,以创建更复杂的UI。 使用Framelayout布局时,我们需要注意重叠的顺序,确保每个子视图的可见性,以及确保每个子视图都有正确的大小和位置。我非常喜欢使用Framelayout布局来构建Android应用的复杂用户界面,它为我们提供了一个强大而灵活的工具,使我们能够创建动态而美丽的UI。 <h3>回答3:</h3><br/>Android布局通常由LinearLayout,RelativeLayout,GridLayout和FrameLayout组成。其中,FrameLayout是最常用的布局之一。FrameLayout是一个布局类,允许您将多个子视图放置在一个单独的视图中,并使用简单的轻量级方式来控制它们的位置和大小。 FrameLayout是一个非常简单的布局,它只是在屏幕上按照子视图排列,没有其他布局,它的根元素是FrameLayoutFrameLayout的特点: 1. FrameLayout可以让我们在同一个屏幕上用多个视图叠放在一起。 2. FrameLayout只会使用最上层的视图,也就是覆盖上面的视图。 3. FrameLayout的每一个子视图都是按照原始大小排列在屏幕上的,并且它们的位置是相对于FrameLayout的左上角而言的。 4. FrameLayout适用于一些小组件布局,比如“确定”和“取消”按钮、气泡等。 FrameLayout的使用方法: 1. 在Activity的XML文件中,建立一个FrameLayout标签。 2. 在FrameLayout标签中添加子视图。 3. 使用android:layout_gravity属性设置子视图的位置和对齐方式。 例如:如果您想让一个ImageView占据整个屏幕并覆盖其他视图,则可以使用如下代码实现: <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/background_image" /> </FrameLayout> 上述代码创建了一个FrameLayout,并在其中包含了一个ImageView视图。ImageView的布局宽度和高度都设置为match_parent,这意味着它将占据整个屏幕的宽度和高度。因此,既可以显示“background_image”图片,也可以覆盖其他视图。 总之,FrameLayout是一种非常常用的布局,在Android应用程序中可以发挥重要作用。您可以使用它的轻量级方式进行布局,以使您的应用程序更加高效、灵活和简单。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值