Android 在子类中定义参数后,如何通过Super发送参数?
我想通过super将属性发送到视图中。(如第一行所示)但IDE不想接受它;我的代码中缺少一些内容Android 在子类中定义参数后,如何通过Super发送参数?,android,kotlin,android-view,android-custom-view,Android,Kotlin,Android View,Android Custom View,我想通过super将属性发送到视图中。(如第一行所示)但IDE不想接受它;我的代码中缺少一些内容 class BoxDrawingView(context: Context): View(context, attrs) { var attrs: AttributeSet? = null constructor(context: Context, attrs: AttributeSet): this(context) { this.attrs = attrs
class BoxDrawingView(context: Context): View(context, attrs) {
var attrs: AttributeSet? = null
constructor(context: Context, attrs: AttributeSet): this(context) {
this.attrs = attrs
}
}
有人能帮我吗?谢谢 试试这个:
class BoxDrawingView : View {
var attrs: AttributeSet? = null
constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
this.attrs = attrs
}
}
问题是,您没有一个单一的构造函数适用于所有情况。发生这种情况时,您可以避免使用普通的构造函数速记(即,在类声明中内联声明属性),创建多个构造函数,并将每个构造函数委托给不同的超类构造函数
如果希望将attrs
声明为val
而不是var,也可以稍微修改一下:
class BoxDrawingView : View {
val attrs: AttributeSet?
constructor(context: Context) : super(context) {
this.attrs = null
}
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
this.attrs = attrs
}
}
试试这个:
class BoxDrawingView : View {
var attrs: AttributeSet? = null
constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
this.attrs = attrs
}
}
问题是,您没有一个单一的构造函数适用于所有情况。发生这种情况时,您可以避免使用普通的构造函数速记(即,在类声明中内联声明属性),创建多个构造函数,并将每个构造函数委托给不同的超类构造函数
如果希望将attrs
声明为val
而不是var,也可以稍微修改一下:
class BoxDrawingView : View {
val attrs: AttributeSet?
constructor(context: Context) : super(context) {
this.attrs = null
}
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
this.attrs = attrs
}
}
先生,似乎可以。谢谢先生,似乎可以。谢谢