最近学习android开发,记录学习过程,分享一写小案例
一. 如下先设置好布局文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
<TextView android:id= "@+id/textView1"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:layout_alignParentTop= "true"
android:layout_centerHorizontal= "true"
android:layout_marginTop= "59dp"
android:text= "欢迎使用"
android:textSize= "40sp"
/>
<Button
android:id= "@+id/button1"
android:onClick= "clickRed"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:layout_alignParentLeft= "true"
android:layout_centerVertical= "true"
android:text= "红"
/>
<Button
android:id= "@+id/button2"
android:onClick= "clickGreen"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:layout_alignBaseline= "@+id/button1"
android:layout_alignBottom= "@+id/button1"
android:layout_centerHorizontal= "true"
android:text= "绿"
/>
<Button
android:id= "@+id/button3"
android:onClick= "clickBlue"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:layout_alignBaseline= "@+id/button2"
android:layout_alignBottom= "@+id/button2"
android:layout_alignRight= "@+id/textView1"
android:text= "蓝"
/>
|
二 关键java代码 设置用getWindow().setBackgroundDrawable设置全局颜色
1
2
3
4
5
6
7
8
9
10
11
12
|
public void clickRed(View view) {
getWindow().setBackgroundDrawable( new
ColorDrawable(Color.RED));
} public void clickBlue(View view) {
getWindow().setBackgroundDrawable( new
ColorDrawable(Color.BLUE));
} public
void clickGreen(View view) {
getWindow().setBackgroundDrawable( new
ColorDrawable(Color.GREEN));
} |
运行效果 如下
源码在这里 喜欢的拿去研究 点击下载