android自定义控件(二) 入门,继承View

转载请注明地址:http://blog.csdn.net/ethan_xue/article/details/7313788

ps: 可根据apidemo里LableView,list4,list6学习

文档在dev guide/Framework Topics/User Interface/Building Custom Components


自定义控件的步骤:

1 View的工作原理 
2 编写View类 
3 为View类增加属性 
4 绘制屏幕 
5 响应用户消息 
6 自定义回调函数 


不多说,主要是Canvas, Paint, Path

[java]  view plain copy
  1. public class CustomView1 extends View {  
  2.   
  3.     private Paint mPaint;  
  4.     // private static final String mText = "drawText";  
  5.     private String mText = "drawText";  
  6.   
  7.     public CustomView1(Context context, AttributeSet attrs) {  
  8.         super(context, attrs);  
  9.     }  
  10.   
  11.     @Override  
  12.     protected void onDraw(Canvas canvas) {  
  13.         super.onDraw(canvas);  
  14.         mPaint = new Paint();  
  15.         mPaint.setColor(Color.BLUE);  
  16.         // FILL填充, STROKE描边,FILL_AND_STROKE填充和描边  
  17.         mPaint.setStyle(Style.FILL);  
  18.         canvas.drawRect(new Rect(1010100100), mPaint);// 画一个矩形  
  19.   
  20.         mPaint.setColor(Color.GREEN);  
  21.         mPaint.setTextSize(35.0f);  
  22.         canvas.drawText(mText, 1060, mPaint);  
  23.     }  
  24.   
  25. }  


布局

[java]  view plain copy
  1. <pre name="code" class="java"><?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <ethan.customview1.CustomView1    
  8.     android:layout_width="wrap_content"   
  9.     android:layout_height="wrap_content"   
  10.     />  
  11. </LinearLayout></pre><pre name="code" class="java"></pre>  
效果图

注意,此时,在控件下放一个textView的话,是显示不出来的(TextView放在控件上面可以显示),以后再解决

下载地址 http://download.csdn.net/detail/ethan_xue/4108820

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值