主Activity类的代码:

import android.app.Activity;import android.content.res.Resources;import android.graphics.Color;import android.graphics.drawable.Drawable;import android.os.Bundle;import android.widget.TextView;public class EX03_03 extends Activity{private TextView mTextView01;private TextView mTextView02;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);mTextView01 = (TextView) findViewById(R.id.myTextView01);mTextView01.setText("我是套用Drawable背景色的戴维文字。");Resources resources = getBaseContext().getResources();Drawable HippoDrawable = resources.getDrawable(R.drawable.white);mTextView01.setBackgroundDrawable(HippoDrawable);mTextView02 = (TextView) findViewById(R.id.myTextView02);mTextView02.setTextColor(Color.MAGENTA);}}

    其中:

Resources resources = getBaseContext().getResources();Drawable HippoDrawable = resources.getDrawable(R.drawable.white);mTextView01.setBackgroundDrawable(HippoDrawable);

    先获取Resources类,再获取Drawable类,通过setBackgroundDrawable()函数将TextView的背景色设置成R.drawable.white索引的颜色值,R.drawable.white的颜色值实际在res/values/color.xml中定义:

#808080FF
#FFFFFFFF

    Activity中的第23行代码通过setTextColor()函数将文本的颜色设置为紫红色:

    mTextView02.setTextColor(Color.MAGENTA);

    这里用到了graphics.Color中的颜色常数。

    Android.graphics.Color中定义的颜色常数如表:

 

类型 常数 色码
int BLACK -16777216 0XFF000000
int BLUE -16776961 0XFF0000FF
int CYAN -16711681 0XFF00FFFF
int DKGRAY -12303292 0XFF444444
int GRAY -7829368 0XFF888888
int GREEN -16711936 0XFF00FF00
int LTGRAY -3355444 0XFFCCCCCC
int MAGENTA -65281 0XFFFF00FF
int RED -65536 0XFFFF0000
int TRANSPARENT 0 0X00000000
int WHITE -1 0XFFFFFFFF
int YELLOW -256 0XFFFFFF00

程序运行截图: