帧布局:
每一个组件为一帧,整个布局就是每个组件一个个之间的叠加。
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/view1"
android:layout_gravity="center"
android:width="320dp"
android:height="320dp"
android:background="#0f0"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/view2"
android:layout_gravity="center"
android:width="280dp"
android:height="280dp"
android:background="#f00"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/view3"
android:layout_gravity="center"
android:width="240dp"
android:height="240dp"
android:background="#ff0"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/view4"
android:layout_gravity="center"
android:width="200dp"
android:height="200dp"
android:background="#0ff"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/view5"
android:layout_gravity="center"
android:width="160dp"
android:height="160dp"
android:background="#f0f"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/view6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:width="120dp"
android:height="120dp"
android:background="#000" />
</FrameLayout>
实现霓虹灯效果:
public class MainActivity extends AppCompatActivity {
int [] names = new int[]{
R.id.view1, R.id.view2, R.id.view3, R.id.view4, R.id.view5, R.id.view6
};
TextView [] views = new TextView[names.length];
class MyHandler extends Handler{
private WeakReference<MainActivity> activity;
public MyHandler(WeakReference<MainActivity> activity){
this.activity = activity;
}
private int currentColor = 0;
//定义一个颜色数组
int[] colors = new int[]{R.color.c1,R.color.c2,R.color.c3,R.color.c4,R.color.c5,R.color.c6};
@Override
public void handleMessage(Message msg) {
if(msg.what == 0x123){
//在for循环里定义两个变量
for (int i = 0, len = activity.get().names.length;i< len;i++ ){
activity.get().views[i].setBackgroundResource(colors[(i + currentColor) % colors.length]);
}
currentColor++;
}
super.handleMessage(msg);
}
}
private Handler handler = new MyHandler(new WeakReference<MainActivity>(this));
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for (int i = 0; i<names.length; i++){
views[i] = findViewById(names[i]);
}
new Timer().schedule(new TimerTask() {
@Override
public void run() {
handler.sendEmptyMessage(0x123);
}
},0,200);
}
}
遇到多个组件需要findId,可以将组件的id放置于一个数组中,将组件放置于一个数组中。通过一个for循环将二者绑定:
int [] names = new int[]{
R.id.view1, R.id.view2, R.id.view3, R.id.view4, R.id.view5, R.id.view6
};
TextView [] views = new TextView[names.length];
for (int i = 0; i<names.length; i++){
views[i] = findViewById(names[i]);
}
约束布局:
相对布局的改进,有点难,先不学。
TextView大量的XML属性:
为文字添加图片
android:drawableRight="@mipmap/ic_launcher"
文字太长可缩略
android:ellipsize="middle"
为文字添加阴影
android:shadowColor="#0f0"
android:shadowDx="10"
android:shadowDy="8"
android:shadowRadius="3"
用XML文件设置TextView的背景
xml文件
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:shape="rectangle">
<!-- 指定圆角矩形的四个圆角半径-->
<corners android:topLeftRadius="20dp"
android:topRightRadius="40dp"
android:bottomRightRadius="10dp"
android:bottomLeftRadius="60dp"/>
<!--指定边框线条颜色-->
<stroke android:width="4px"
android:color="#f0f"/>
<!--指定使用渐变背景色,使用sweep类型的渐变
颜色:红-绿-蓝-->
<gradient
android:startColor="#f00"
android:centerColor="#0f0"
android:endColor="#00f"
android:type="sweep"/>
</shape>
设置背景:
<TextView
android:text="圆角边框,渐变背景"
android:textSize="24pt"
android:background="@layout/bg22"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
时钟:AnalogClock TextClock
<AnalogClock
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextClock
android:textSize="20sp"
android:textColor="#f0f"
android:format24Hour="yyyy年 MM月 dd日 HH:mm :ss"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
计时器:Chronometer