Des*_*tor 4 android extend kotlin
我希望ConstaintLayout带有额外的其他属性,但是我很难扩展它。更确切地说,我在将正确的构造函数放入时遇到了麻烦
class myCL(): ConstraintLayout(???) {
}
Run Code Online (Sandbox Code Playgroud)
为了确保您在行为上没有任何怪异,您应该像这样实现它。
class myCL: ConstraintLayout {
constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
constructor(context: Context, attrs: AttributeSet?,
@AttrRes defStyleAttr: Int) : super(context, attrs, defStyleAttr)
}
Run Code Online (Sandbox Code Playgroud)
您真正需要的构造函数是带有所有参数的构造函数:
class myCL(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) :
ConstraintLayout(context, attrs, defStyleAttr) {
}
Run Code Online (Sandbox Code Playgroud)
如果您想轻松地实现所有三个构造函数,则可以使用@JvmOverloads
并使用明智的默认值。
class myCL @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : ConstraintLayout(context, attrs, defStyleAttr) {
}
Run Code Online (Sandbox Code Playgroud)
参见https://developer.android.com/reference/android/support/constraint/ConstraintLayout
归档时间: |
|
查看次数: |
1150 次 |
最近记录: |