您的位置:首页 > 移动开发 > Android开发

Android初级教程九——更改手机窗口画面底色

2011-09-19 01:05 351 查看
① 编写main 布局

<?xml version="1.0" encoding="utf-8"?>

<AbsoluteLayout

android:id="@+id/widget0"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

xmlns:android="http://schemas.android.com/apk/res/android"

> <

TextView

android:id="@+id/name"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="账号"

android:layout_x="61px"

android:layout_y="69px"

> </TextView>

<TextView

android:id="@+id/password"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="密码"

android:layout_x="61px"

android:layout_y="158px"

> </TextView>

<EditText

android:id="@+id/name_in"

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/pwd_in"

android:layout_width="120dip"

android:layout_height="wrap_content"

② 在values 文件夹中定义一个drawable.xml 文件用来存放颜色值

<?xml version="1.0" encoding="utf-8"?>

<resources>

<color name="white">#FFFFFF</color>

<color name="darkgray">#938192</color>

<color name="lightgreen">#7cd12e</color>

</resources>

③ 修改main.xml 中的屏幕背景颜色和TextView 的字体颜色

<AbsoluteLayout

android:id="@+id/widget0"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:background="@color/white"

xmlns:android="http://schemas.android.com/apk/res/android"

> <

TextView

android:id="@+id/name"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="账号"

android:textColor="@color/darkgray"

android:layout_x="61px"

android:layout_y="69px"

> </TextView>

<TextView

android:id="@+id/password"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="密码"

android:textColor="@color/darkgray"

android:layout_x="61px"

android:layout_y="158px"

>

④ 在mainActivity.java 代码中修改TextView 背景颜色

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

TextView text=(TextView)findViewById(R.id.name);

//由ID获得资源

Resources myColor=getBaseContext().getResources();

//getBaseContext()获得基础Context

//getResources()获得资源

Drawable color_M=myColor.getDrawable(R.color.lightgreen );

//由资源myColor来获得Drawable R.color.lightgreen是颜色值的ID引用

text.setBackgroundDrawable(color_M);

//设置背景

}

⑤ 结果:略
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息