效果图(会闪烁):
xml文件:
设置一个6*4的网格布局,在第一行中设置一个文本框,第二行中设置一个横跨四列的清零按钮。
java文件:
创建16个按钮文本,每个文本对应一个按钮,放到指定的行和列即可。
霓虹灯效果:
在values资源文件中定义colors.xml文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#008577</color>
<color name="colorPrimaryDark">#00574B</color>
<color name="colorAccent">#D81B60</color>
<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>
<color name="color7">#f00</color>
<color name="color8">#0f0</color>
<color name="color9">#00f</color>
<color name="color10">#ff0</color>
<color name="color11">#f0f</color>
<color name="color12">#0ff</color>
</resources>
在Java文件中定义一个线程周期性地改变颜色的值,android中的线程是不安全的,且线程通常会完成耗时的工作,为了不影响用户体验,所以要将线程中处理的事件放在Handle中。在线程中的run方法中每隔1s发送一个事件,handle接收此事件改变颜色。
源码附上:
xml文件:
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:rowCount="6"
android:columnCount="4"
android:id="@+id/root">
<!--定义横跨4列的文本框-->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_columnSpan="4"
android:textSize="50sp"
android:layout_marginLeft="2pt"
android:layout_marginRight="2pt"
android:padding="3pt"
android:layout_gravity="right"
android:background="#eee"
android:textColor="#000"
android:text="0" />
<!--定义一个横跨4列的按钮-->
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_columnSpan="4"
android:text="清除" />
</GridLayout>
java文件:
package com.example.a73435.myapplication;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.Button;
import android.widget.GridLayout;
import java.util.Timer;
import java.util.TimerTask;
import static android.widget.GridLayout.*;
public class MainActivity extends AppCompatActivity {
GridLayout girdLayout;
String[] chars = new String[]//定义16个按钮的文本
{
"7", "8", "9", "÷",
"4", "5", "6", "×",
"1", "2", "3", "-",
".", "0", "=", "+",
};
Button []bns=new Button[chars.length];
private int currentColor=0;
final int[] colors=new int[]{
R.color.color1,
R.color.color2,
R.color.color3,
R.color.color4,
R.color.color5,
R.color.color6,
};
Handler handler=new Handler()
{
@Override
public void handleMessage(Message msg)//等待接收时间
{
if(msg.what==0x123)//表明本消息来自本程序发送的
{
for(int i=0;i<chars.length;i++)
{
bns[i].setBackgroundResource(colors[(i+currentColor)%6]);
}
currentColor++;
}
super.handleMessage(msg);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
girdLayout = (GridLayout) findViewById(R.id.root);
for (int i = 0; i < chars.length; i++) {
Button bn = new Button(this);
bns[i]=bn;
bn.setText(chars[i]);
bn.setTextSize(40);
bn.setPadding(5, 82, 3, 82);//设置按钮四周空白区域
Spec rowSpec = spec(i / 4 + 2);//指定该组件所在的行
Spec columnSpec = spec(i % 4);//指定该组件所在的列
GridLayout.LayoutParams params = new GridLayout.LayoutParams(rowSpec, columnSpec);
params.setGravity(Gravity.FILL);//指定该组件占满整个父容器
girdLayout.addView(bn, params);
}
new Timer().schedule(new TimerTask()
{
@Override
public void run()
{
handler.sendEmptyMessage(0x123);
}
},0,1000);//发送一条空消息通知系统改变按钮的背景色
}
}
android视图与五大布局:https://blog.csdn.net/LoverLeslie/article/details/80601469