自定义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,主题中有对某些属性的默认赋值。
在这里插入图片描述
或者利用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);
}
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
使用自定义View可以让你在Android应用中创建具有自定义外观和交互行为的组件。以下是使用自定义View的基本步骤: 1. 创建一个继承自View或其子类的Java类,作为自定义View的实现。 例如,你可以创建一个名为CustomView的类:`public class CustomView extends View` 2. 在自定义View构造函数中进行初始化和设置。 在构造函数中,你可以设置画笔、颜色、形状、属性等。你还可以处理触摸事件、测量和布局等。 例如,你可以在构造函数中初始化画笔:`Paint paint = new Paint();` 3. 重写onDraw()方法,在该方法中实现自定义View的绘制逻辑。 在onDraw()方法中,你可以使用Canvas对象绘制图形、文本等元素。通过调用invalidate()方法,可以触发View的重新绘制。 例如,你可以在onDraw()方法中使用画笔绘制一个圆:`canvas.drawCircle(x, y, radius, paint);` 4. 根据需要重写其他的方法,处理触摸事件、测量和布局等。 例如,你可以重写onTouchEvent()方法来处理触摸事件,并根据手势改变自定义View的状态。 5. 在XML布局文件或代码中使用自定义View。 如果你希望在XML布局文件中使用自定义View,可以在XML文件中添加相应的标签,并指定自定义View的包名和类名。 例如,你可以在XML布局中添加一个CustomView:`<com.example.app.CustomView android:layout_width="match_parent" android:layout_height="match_parent"/>` 以上是使用自定义View的基本步骤。当你使用自定义View时,可以根据自己的需求定制其外观和行为,并根据需要处理相关事件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值