android自定义View-继承
来源:互联网 发布:一折特卖淘宝授权 编辑:程序博客网 时间:2024/05/19 05:30
介绍anroid通过继承系统的控件自定义view
方法是通过对OnDraw()方法进行复写来实现的
举例继承TextView
在textView的背景加上矩形的效果
代码实现
testView的代码
public class TestView extends TextView { public TestView(Context context) { super(context); } @Override protected void onDraw(Canvas canvas) { Paint paint1 = new Paint(); paint1.setColor(getResources().getColor(android.R.color.holo_blue_light)); paint1.setStyle(Paint.Style.FILL); Paint paint2 = new Paint(); paint2.setColor(Color.YELLOW); paint2.setStyle(Paint.Style.FILL); canvas.drawRect(0,0,getMeasuredWidth(),getMeasuredHeight(),paint1); canvas.drawRect(10,10,getMeasuredWidth()-10,getMeasuredHeight()-10,paint2); canvas.save(); canvas.translate(10,0); super.onDraw(canvas); canvas.restore(); }}
布局的代码
public class TestView extends TextView { public TestView(Context context) { super(context); } @Override protected void onDraw(Canvas canvas) { Paint paint1 = new Paint(); paint1.setColor(getResources().getColor(android.R.color.holo_blue_light)); paint1.setStyle(Paint.Style.FILL); Paint paint2 = new Paint(); paint2.setColor(Color.YELLOW); paint2.setStyle(Paint.Style.FILL); canvas.drawRect(0,0,getMeasuredWidth(),getMeasuredHeight(),paint1); canvas.drawRect(10,10,getMeasuredWidth()-10,getMeasuredHeight()-10,paint2); canvas.save(); canvas.translate(10,0); super.onDraw(canvas); canvas.restore(); }}
我的微信二维码如下,欢迎交流讨论
欢迎关注《IT面试题汇总》微信订阅号。每天推送经典面试题和面试心得技巧
微信订阅号二维码如下:
0 0
- android自定义View-继承
- Android自定义View(三)继承View
- Android 自定义属性 view (继承View)
- android 自定义view(二),继承view
- 自定义View继承view
- 自定义View---继承View
- 自定义View,继承View
- 自定义view继承view
- android自定义控件(二) 入门,继承View
- android自定义控件(二) 入门,继承View
- Android-自定义控件之继承View
- android自定义控件(二) 入门,继承View
- android自定义 ProgressBar(继承自View)
- android自定义圆形头像view,继承imageview
- android自定义控件(二) 入门,继承View
- Android自定义View(四)继承ViewGroup
- Android 如何自定义控件(继承View)
- Android 自定义开关(继承View)
-  |"|&|<|>等字符转义
- stm8使用printf问题
- 字面量,常量和变量之间的区别?
- 盘点:全球12个大数据公司
- 90%的人跑步都白跑了!跑步的正确姿势和膝盖保养知
- android自定义View-继承
- 0x669586df (QtCored4.dll) 处最可能的异常: 0xC0000005: 写入位置 0xabababab 时发生访问冲突
- 基于iscroll实现下拉和上拉刷新
- progressbar样式的一点小细节
- 关于Ubuntu14.04安装配置tftp
- 3种快排以及四种优化
- JDK 安装
- linux 下 /bin /sbin 的区别
- 初学者如何查阅自然语言处理(NLP)领域学术资料