http://topic.csdn.net/u/20101127/00/d7cbe737-f9ef-4a19-ae0e-b61f7b447d5f.html
public class AndroidTest extends Activity { private LinearLayout layout; private LinearLayout.LayoutParams params1, params2; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); layout = (LinearLayout)findViewById(R.id.rootLayout); params1 = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); params2 = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); MyView myView1 = new MyView(this, 100, 100, 50); myView1.setColor(Color.MAGENTA); myView1.layout(50, 50, 150, 150); myView1.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub setTitle("MyView"); } }); layout.addView(myView1, params1); MyView smallCircle = new MyView(this, 125, 200, 25); smallCircle.setColor(Color.GREEN); smallCircle.layout(100, 175, 150, 225); layout.addView(smallCircle, params2); } public class MyView extends View { private float m_cx; private float m_cy; private float m_radius; private Paint paint; MyView(Context context, float cx, float cy, float radius) { super(context); m_cx = cx; m_cy = cy; m_radius = radius; // 首先定义一个paint paint = new Paint(); // 绘制矩形区域-实心矩形 // 设置颜色 paint.setColor(Color.BLUE); // 设置样式-填充 paint.setStyle(Style.FILL); } public void setColor(int color) { // 设置颜色 paint.setColor(color); } @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); // draw circle canvas.drawCircle(m_cx, m_cy, m_radius, paint); } } }