窗口的底色是“深黑色”,这是SDK默认的颜色,要更改Activity里的窗口底色有许多方法,最简单的方法就是将颜色色码事先定义在color中,当程序onCreate创建的同时,加载预先定义的画面颜色。
此范例程序的设计方式是在color里指定Layout的背景(BackGround)为粉色,但这里的粉色颜色色码为#FFC0CB)预先定义在color当中,当程序运行时,背景就会变为粉色。
运行结果:
操作步骤:
step1:新建Android工程EX03_02_BackgroundColorDemo
step2:res->values下添加color.xml
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<color name="white">#FFFFFF</color>
<color name="black">#000000</color>
<color name="red">#FF0000</color>
<color name="darkgray">#A9A9A9</color>
<color name="lightskyblue">#87CEFA</color>
<color name="pink">#FFC0CB</color>
<color name="lightpink">#FFB6C1</color>
<color name="darkblue">#00008B</color>
</resources>
step3:res->values->strings.xml
<resources>
<string name="app_name">EX03_02_BackgroundColorDemo</string>
<string name="hello_world">Hello world,EX03_02_BackgroundColorDemo!</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_ex03_02__background_color_demo">EX03_02_BackgroundColorDemo</string>
<string name="number">账号</string>
<string name="pwd">密码</string>
</resources>
step4:res->layout->main.xml
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/pink">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/number"
android:textColor="@color/darkblue"
android:layout_x="61px"
android:layout_y="69px"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/pwd"
android:textColor="@color/darkblue"
android:layout_x="61px"
android:layout_y="158px"/>
<EditText
android:layout_width="120dip"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_x="114px"
android:layout_y="57px"/>
<EditText
android:layout_width="120dip"
android:layout_height="wrap_content"
android:textSize="18sp"
android:password="true"
android:layout_x="112px"
android:layout_y="142px"/>
</AbsoluteLayout>
step5:EX03_02_BackgroundColorDemo.java
package com.example.ex03_02_backgroundcolordemo;
import android.os.Bundle;
import android.app.Activity;
public class EX03_02_BackgroundColorDemo extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}