自定义View的四个构造函数

自定义View继承View或者ViewGroup都会让我们实现构造函数,通常会实现一个参数的构造函数,两个参数的构造函数和三个参数的构造函数,它们有什么区别,又为什么要实现这么多构造函数呢?

public class DemoView77 extends View { 

    public DemoView77(Context context) { 
        super(context);
        System.out.println("=========一个参数============");
    }

    public DemoView77(Context context, @Nullable AttributeSet attrs) { 
        super(context, attrs);
        System.out.println("=========两个参数============");
    }

    public DemoView77(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { 
        super(context, attrs, defStyleAttr);
        System.out.println("=========三个参数============");
    }

    public DemoView77(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) { 
        super(context, attrs, defStyleAttr, defStyleRes);
        System.out.println("=========四个参数============");
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}

调用说明:
利用代码直接new 布局时会调用一个参数的构造函数,
如果直接写在xml文件中会调用二个参数的构造函数被调用。
上面的写法永远不会调用第三个,第四个构造函数,三个,四个参数的构造函数通常由我们自己主动调用.

public class DemoView77 extends View { 

    public DemoView77(Context context) { 
        this(context,null);
        System.out.println("=========一个参数============");
    }

    public DemoView77(Context context, @Nullable AttributeSet attrs) { 
        this(context, attrs,R.style.);
        System.out.println("=========两个参数============");
    }

    public DemoView77(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { 
        super(context, attrs, defStyleAttr);
        System.out.println("=========三个参数============");
    }
}

Android中的View在xml文件中使用的属性tag,大部分是系统定义好的,同时我们可以自定义属性。

构造函数参数说明:
Context – 上下文;
AttributeSet – xml文件中的属性;
int defStyleAttr – Theme中的默认样式;
int defStyleResource – defStyleAttr未使用(为0,或者未匹配到),则应用于View的默认样式;
R.style中系统为view定义了很多默认主题Theme,主题中有对某些属性的默认赋值。
《自定义View的四个构造函数》
或者利用com.android.internal.R.attr.buttonStyle设置默认属性值。

//代码初始化时会用到
public View(Context context)
//加载xml布局文件时会被调用,tag属性存储在AttributeSet中,自定义的属性会存放在attrs中
public View(Context context, @Nullable AttributeSet attrs) { 
    this(context, attrs, 0);
}

 @param defStyleAttr An attribute in the current theme that contains a
*        reference to a style resource that supplies default values for
*        the view. Can be 0 to not look for defaults.

//可以为application和activity设置theme,theme中包含了view的某些属性的默认值。也可以手动设置defStyleAttr 。
//会从defStyleAttr (从theme中获取或者指定)中查找默认满足条件的属性的值,如果defStyleAttr 为0则不会查找属性赋值默认的值
public View(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { 
    this(context, attrs, defStyleAttr, 0);
}


* @param defStyleAttr An attribute in the current theme that contains a
*        reference to a style resource that supplies default values for
*        the view. Can be 0 to not look for defaults.
* @param defStyleRes A resource identifier of a style resource that
*        supplies default values for the view, used only if
*        defStyleAttr is 0 or can not be found in the theme. Can be 0
*        to not look for defaults.

//第四个参数是一个style的资源引用(为view设置style属性),支持view的默认值,只有第三个参数为0或者未在theme中设置时才会生效,这个构造函数几乎不会用到
public View(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { 
    this(context, attrs, defStyleAttr, 0);
}

说明:

  • 一个参数的构造函数:View或者ViewGroup可以利用代码直接new对象,这时就会调用一个参数的构造函数,生成对象之后利用内部提供的属性设置方法就行属性设置。
  • 两个参数的构造函数:用于加载xml布局文件时调用,通常自定义控件的自定义属性的读取需要用到这个构造函数(系统的View,ViewGroup只要写在xml中也会调用这个构造函数)。
  • 三个参数的构造函数:一般系统不会主动调用,需要手动调用,可以手动传入defStyleAttr并调用,即时在view中定义了them,style,也不会调用三参构造函数。
  • 四个参数的构造函数,如果第三个参数为0或者没有定义defStyleAttr时,第四个参数才起作用,它是style的引用,高版本才支持,所以一般不会用到。

一般定义view时,View的四个构造函数时相互调用的,调用其中的一个内部会调用相应的其他构造函数,例如Button,Button内部指定了默认的com.android.internal.R.attr.buttonStyle

很多View都有默认的属性值:

public Button(Context context) { 
    this(context, null);
}

public Button(Context context, AttributeSet attrs) { 
    this(context, attrs, com.android.internal.R.attr.buttonStyle);
}

public Button(Context context, AttributeSet attrs, int defStyleAttr) { 
    this(context, attrs, defStyleAttr, 0);
}

public Button(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 
    super(context, attrs, defStyleAttr, defStyleRes);
}
    原文作者:lidongxiu0714
    原文地址: https://blog.csdn.net/u010126792/article/details/88890930
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞