指定Layout背景最簡(jiǎn)單的方法,通常是把顏色的常數(shù)定義在darwable當(dāng)中,這個(gè)范例我們要把背景變成白色。
設(shè)計(jì)思路:
- 在values下面新建一個(gè)color.xml,定義兩個(gè)顏色,一個(gè)是白色name="white",值為#ffffffff。一個(gè)是灰色name="darkgray",值為#808080FF。
- 對(duì)main.xml布局文件增加兩個(gè)TextView,和兩個(gè)EditText。TextView使用android:textColor設(shè)置顏色為darkgray。設(shè)置全局背景android:background顏色為white。并且全局使用絕對(duì)定位。
代碼如下:
EX03_02.javapackage dan.ex03_02;
import android.app.Activity;
import android.os.Bundle;
public class EX03_02 extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
main.xml<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/widget35"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/white"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<TextView
android:id="@+id/widget28"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/str_id"
android:textColor="@drawable/darkgray"
android:layout_x="61px"
android:layout_y="69px"
>
</TextView>
<TextView
android:id="@+id/widget29"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/str_psw"
android:textColor="@drawable/darkgray"
android:layout_x="61px"
android:layout_y="158px"
>
</TextView>
<EditText
android:id="@+id/widget31"
android:layout_width="120dip"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_x="114px"
android:layout_y="57px"
>
</EditText>
<EditText
android:id="@+id/widget30"
android:layout_width="120dip"
android:layout_height="wrap_content"
android:textSize="18sp"
android:password="true"
android:layout_x="112px"
android:layout_y="142px"
>
</EditText>
</AbsoluteLayout>
string.xml<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, EX03_02!</string>
<string name="app_name">EX03_02</string>
<string name="str_id">賬號(hào)</string>
<string name="str_psw">密碼</string>
</resources>
color.xml<resources>
<drawable name="darkgray">#808080FF</drawable>
<drawable name="white">#FFFFFFFF</drawable>
</resources>
運(yùn)行效果如下: