Android:自定义View 初步之旅随记(一)

来源:互联网 发布:ff14帅气女性捏脸数据 编辑:程序博客网 时间:2024/05/06 15:55
自定义View 初步之旅1)自定义MEditText1 MEditText extends EditText(ImageView)2 可以实现三个构造器:   public MEditText(Context context, AttributeSet attrs, int defStyle) {      super(context, attrs, defStyle);// 在布局文件中 声明 加上主题      init(context, attrs);   }   public MEditText(Context context, AttributeSet attrs) {      this(context, attrs, 0);// 在布局文件中 声明   }   public MEditText(Context context) {      this(context, null); // 在代码中  MEditText e=new MEditTExt(context);   }   3 init 方法 获取布局文件的控件自定义属性  在此文件中声明:values/attrs.xml   <declare-styleable name="NoteEdit">        <!-- 定义属性名称,这个就是用在layout中的 -->        <!--           代码获取这个属性应该这样获取:              color=typedArray.getColor(R.styleable.NoteEdit_noteLineColor,ColorBlack);        -->        <attr name="noteLineColor" format="color"></attr>    </declare-styleable>  在布局文件中:   加个命名空间: xmlns:app="http://schemas.android.com/apk/res/包名"   <com.example.thousanphone_1506.widget.MEditText        app:noteLineColor="#5DA7F0" />         在代码中获取自定义的属性: // 获取定义的属性集合,注意,obtainStyledAttributes 第一个参数是传递过来的属性集合// 第二个参数是在 attrs.xml 的<declare-styleable name="NoteEidt"> 的内容,这样再获取      TypedArray typeArray = context.obtainStyledAttributes(attrs,            R.styleable.NoteEdit);      if (typeArray != null) {         defaultColor = typeArray.getColor(               R.styleable.NoteEdit_noteLineColor, defaultColor);      } 4 onDraw(Canvas canvas)   canvas.drawLine(rect.left, currentY, rect.right, currentY, paint);2)Paint:颜料两个需要注意点:holds the style and color,how to draw geometries(几何图形), text(文本) and bitmaps(图片).3)Canvas4)Rect
1 0
原创粉丝点击