前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android开发笔记(十一)自定义视图的构造方法

Android开发笔记(十一)自定义视图的构造方法

作者头像
aqi00
发布2019-01-18 10:32:41
6080
发布2019-01-18 10:32:41
举报
文章被收录于专栏:老欧说安卓老欧说安卓

自定义视图的用法

Android自带的视图常常不能满足实际开发的需求,这种情况下我们就得自定义视图(View)。 首先在res\values目录下找到attrs.xml(如没有则创建之),在该属性定义文件的resources根节点下增加类似下面的定义:

代码语言:javascript
复制
    <declare-styleable name="SignatureView">
        <attr name="paint_color" format="color" />
    </declare-styleable>

其次在代码中创建类似SignatureView的自定义视图类,编写代码并编译通过。 然后在使用自定义视图的布局文件的根节点下增加类似下面的命名空间定义,这里的路径应与AndroidManifest.xml的package属性值保持一致。

代码语言:javascript
复制
    xmlns:app="http://schemas.android.com/apk/res/com.practice.activity"

最后在使用视图的xml布局中加上类似下面这样的xml描述:

代码语言:javascript
复制
    <com.practice.widget.SignatureView
        android:id="@+id/view_signature"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:paint_color="@color/blue" />

自定义视图的编码步骤

自定义视图的编码主要由四部分组成: 一、重写构造函数,并初始化个性化参数; 二、重写测量函数onMesure,计算该视图的宽与高(除了复杂视图,实际开发中一般不进行重写); 三、重写绘图函数onDraw、onLayout、dispatchDraw,视情况重写三个其中的一个或多个; 四、重写触摸事件函数dispatchTouchEvent、onInterceptTouchEvent、onTouchEvent,一般情况不做重写,当然存在手势滑动冲突时,就必须重写; 以上四点,本章只记录前面三点,第四点事件函数这部分内容较复杂,留待后面的章节介绍。

三种构造函数的区别

自定义视图存在三个构造函数,分别是

代码语言:javascript
复制
		//只有一个参数,用于在代码中构造对象
    public SignatureView(Context context) {
        super(context);
    }
  
		//有两个参数,用于在XML布局中构造对象
    public SignatureView(Context context, AttributeSet attrs) {
        super(context, attrs);
	if (attrs != null) {
            TypedArray attrArray=getContext().obtainStyledAttributes( attrs, R.styleable.SignatureView);
            mPaintColor = attrArray.getColor(R.styleable.SignatureView_paint_color, 0);
            attrArray.recycle();
        }
    }
    
		//有三个参数,用于在XML布局中构造对象
    public SignatureView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

其中,在代码中构造对象时采用第一种构造函数,在XML布局中构造对象时采用第二种构造函数。第三个函数也是在XML布局中构造对象时使用,它与第二种的区别在于: 1、defStyleAttr是一种特殊的属性,其类型既非整型也非字符串,而是参照类型(reference,需要在style.xml中另外定义),举例如下:

代码语言:javascript
复制
    <declare-styleable name="SignatureView">
        <attr name="paint_color" format="color" />
    </declare-styleable>
    <attr name="CustomizeStyle" format="reference" />

2、XML布局直接调用的都是第二种构造,第三种构造都是通过第二种构造来调用,举例如下:

代码语言:javascript
复制
    public SignatureView(Context context, AttributeSet attrs) {
        this(context, attrs, R.attr.CustomizeStyle);
    }
    
    public SignatureView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
	if (attrs != null) {
            TypedArray attrArray=getContext().obtainStyledAttributes( attrs, R.styleable.SignatureView, defStyleAttr, R.style.DefaultCustomizeStyle);
            mPaintColor = attrArray.getColor(R.styleable.SignatureView_paint_color, 0);
            attrArray.recycle();
        }
    }

这样Android在寻找该自定义视图的属性时,就会依次先找XML布局文件,再找attrs.xml文件中R.attr.CustomizeStyle的定义,最后找style文件中R.style.DefaultCustomizeStyle的定义。

个人感觉第三种构造函数在实际开发中用的不多,不需要过多的深入研究,了解了解就好了。

点此查看Android开发笔记的完整目录

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015年12月04日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 自定义视图的用法
  • 自定义视图的编码步骤
  • 三种构造函数的区别
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档