本文介绍了Android Kotlin-如何扩展ConstraintLayout?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望ConstaintLayout带有其他附加属性,但是我很难扩展它.更确切地说,我在将正确的构造函数放入

I'd like my ConstaintLayout to carry extra additional properties, but I have trouble extending it. More precisely I have trouble putting correct constructor into

class myCL(): ConstraintLayout(???) {
}

推荐答案

您真正需要的构造函数是带有所有参数的构造函数:

The constructor you really need is the one with all arguments:

class myCL(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) :
    ConstraintLayout(context, attrs, defStyleAttr) {
}

如果要轻松实现所有三个构造函数,可以使用 @JvmOverloads 并使用合理的默认值.

if you want to implement all three constructors without much hassle you can use @JvmOverloads and use sensible defaults.

class myCL @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : ConstraintLayout(context, attrs, defStyleAttr) {
}

请参见 https://developer.android.com/reference/android/support/constraint/ConstraintLayout

这篇关于Android Kotlin-如何扩展ConstraintLayout?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 19:47