<?xml version="1.0" encoding="utf-8"?> <resources> <color name="red">#ffff0000</color> </resources>
package gphone.ex03_03; import android.app.Activity; import android.content.res.Resources; import android.os.Bundle; import android.widget.TextView; public class EX03_03 extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Resources resources=this.getBaseContext().getResources(); int color=resources.getColor(R.color.red); TextView tv=(TextView)this.findViewById(R.id.text); tv.setBackgroundColor(color); } }
或颜色在编码时定义使用系统定义颜色:
package gphone.ex03_03; import android.app.Activity; import android.content.res.Resources; import android.os.Bundle; import android.widget.TextView; public class EX03_03 extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); /*Resources resources=this.getBaseContext().getResources(); int color=resources.getColor(R.color.red);*/ TextView tv=(TextView)this.findViewById(R.id.text); tv.setBackgroundColor(android.graphics.Color.RED); } }