1.采用在布局文件中定义默认的背景颜色
首先定义一个颜色的资源文件,Android中所有的资源文件都是以XML的样式定义的
<?xml version="1.0" encoding="utf-8"?>
<resources>
<drawable name="white">#ffffff</drawable>
</resources>
其次,在布局文件中引用ID来访问
代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/white"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
这样引用上面布局文件的Activity的背景就是白色的了,截图如下:
2.可以通过当前基类对象得到资源文件引用,并通过View.setBackgroundDrawable(Drawable b)来设置背景颜色,具体用法如下:
Resources resources = getBaseContext().getResources();
Drawable drawable = resources.getDrawable(R.drawable.white);
TextView tv = (TextView)findViewById(R.id.txt_name);
tv.setBackgroundDrawable(drawable);