隐藏视图的默认构造函数

Kotlin有没有办法隐藏(放置在其他地方)视图的默认构造函数? 也许创建一个子视图或扩展或类似的东西。

目前我所有的观点都是这样的,有点冗长:

class MyView(context: Context, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int): View(context, attrs, defStyleAttr, defStyleRes) { constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int): this(context, attrs, defStyleAttr, 0) constructor(context: Context, attrs: AttributeSet?): this(context, attrs, 0, 0) constructor(context: Context): this(context, null, 0) } 

您可以使用默认参数 :

 class MyView(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0, defStyleRes: Int = 0): View(context, attrs, defStyleAttr, defStyleRes) 

如果您需要从Java调用这些构造函数,请考虑将 @JvmOverloads注释应用于构造函数:

 class MyView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0, defStyleRes: Int = 0): View(context, attrs, defStyleAttr, defStyleRes) 

或多或少正确的格式为@ udalov的答案:

 class MyView(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0, defStyleRes: Int = 0) : View(context, attrs, defStyleAttr, defStyleRes) 

要么

 class MyView @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0, defStyleRes: Int = 0 ) : View(context, attrs, defStyleAttr, defStyleRes) { // .... }