android的帧布局,七、Android帧布局FrameLayout和霓虹灯效果

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

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

XML属性

相关方法

说  明

android:foreground

setForeground(Drawable)

设置该帧布局容器的前景图像

android:foregroundGravity

setForegroundGravity(int)

定义绘制前景图像的gravity属性

下面的布局界面定义使用FrameLayout布局,并向该布局容器中添加了7个TextView,这7个TextView的高度完全相同,而宽度逐渐减少——这保证最先添加的TextView不会被完全遮挡;而且设置了7个TextView的背景色渐变。

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

android:id="@+id/View01"

android:layout_width="210dp"

android:layout_height="200dp"

android:background="#ff0000"/>

android:id="@+id/View02"

android:layout_width="180dp"

android:layout_height="200dp"

android:background="#dd0000"/>

android:id="@+id/View03"

android:layout_width="150dp"

android:layout_height="200dp"

android:background="#bb0000"/>

android:id="@+id/View04"

android:layout_width="120dp"

android:layout_height="200dp"

android:background="#990000"/>

android:id="@+id/View05"

android:layout_width="90dp"

android:layout_height="200dp"

android:background="#770000"/>

android:id="@+id/View06"

android:layout_width="60dp"

android:layout_height="200dp"

android:background="#550000"/>

android:id="@+id/View07"

android:layout_width="30dp"

android:layout_height="200dp"

android:background="#330000"/>

效果如图:

d35783c0bdcf?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

颜色渐变.jpg

霓虹灯效果

如果考虑轮换上面帧布局中的7个TextView的背景色,就会看到上面的颜色渐变条不断地变化,就像大街上的霓虹灯。

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.activity3);

for (int i = 0; i < 7; i++) {

views[i] = findViewById(names[i]);

}

@SuppressLint("HandlerLeak")

final Handler handler = new Handler() {

@Override

public void handleMessage(@NonNull Message msg) {

if (msg.what == 0x1122) {

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变量值

new Timer().schedule(new TimerTask() {

@Override

public void run() {

currentColor++;

if (currentColor >= 6) {

currentColor = 0;

}

Message m = new Message();

m.what = 0x1122;

handler.sendMessage(m);

}

}, 0, 500);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值