Android使用AttributeSet自定义控件的方法

参考资料: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);



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
设计自定义控件需要以下步骤: 1.创建一个新的 Android Studio 项目。 2.在项目中创建自定义控件的类。该类应继承自 View 或其子类(例如 Button、EditText 等)。 3.在自定义控件类中实现构造函数和必要的方法,例如 onMeasure()、onLayout() 和 onDraw() 等。 4.在布局文件中使用自定义控件。 下面是一个示例代码: ``` public class MyCustomView extends View { private Paint paint; private Rect rect; public MyCustomView(Context context) { super(context); init(); } public MyCustomView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public MyCustomView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } private void init() { paint = new Paint(); paint.setColor(Color.BLUE); rect = new Rect(0, 0, 100, 100); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int desiredWidth = 200; int desiredHeight = 200; int widthMode = MeasureSpec.getMode(widthMeasureSpec); int widthSize = MeasureSpec.getSize(widthMeasureSpec); int heightMode = MeasureSpec.getMode(heightMeasureSpec); int heightSize = MeasureSpec.getSize(heightMeasureSpec); int width; int height; if (widthMode == MeasureSpec.EXACTLY) { width = widthSize; } else if (widthMode == MeasureSpec.AT_MOST) { width = Math.min(desiredWidth, widthSize); } else { width = desiredWidth; } if (heightMode == MeasureSpec.EXACTLY) { height = heightSize; } else if (heightMode == MeasureSpec.AT_MOST) { height = Math.min(desiredHeight, heightSize); } else { height = desiredHeight; } setMeasuredDimension(width, height); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawRect(rect, paint); } } ``` 在布局文件中使用自定义控件: ``` <com.example.myapplication.MyCustomView android:layout_width="wrap_content" android:layout_height="wrap_content"/> ``` 以上代码是一个简单的自定义 View,它绘制一个蓝色的矩形。当然,你可以根据需要修改自定义控件的代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值