参考资料:http://www.cnblogs.com/zwl12549/archive/2011/04/13/2015366.html
http://blog.csdn.net/Android_Tutor/article/details/5508615
http://blog.csdn.net/czh0766/article/details/5912237
Android使用AttributeSet自定义控件的方法:
例子:
package www.dianmobile.bill_ming.myview;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.RectF;import android.util.AttributeSet;import android.view.View;public class MyView extends View{ public MyView(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawColor(Color.BLACK); //绘制黑色背景 Paint paint = new Paint(); //创建画笔 paint.setColor(Color.GRAY); //设置画笔颜色 //绘制矩形 canvas.drawRect(10, 10, 110, 110, paint); //字符串,以字符串下面为基准 canvas.drawText("哈哈哈哈哈哈哈", 10, 130, paint); //定义一个矩形 RectF rf1 = new RectF(10,130,110,230); //画弧 顺时针 canvas.drawArc(rf1, 0, 45,true, paint); //画线 canvas.drawLine(150, 10, 250, 110, paint); //定义一个矩形 RectF rf2 = new RectF(150,130,250,230); //画圆 canvas.drawOval(rf2, paint); } }
res下要建一个attr.xml:<?xml version="1.0" encoding="utf-8"?><resources> <declare-styleable name="MyView"> <attr name="sidebuffer" format="integer" /> </declare-styleable></resources>
private MyView mMyView = null;
mMyView = (MyView)findViewById(R.id.myView);
mMyView = (MyView)findViewById(R.id.myView);