gpt4 book ai didi

android - 隐藏 View 默认构造函数

转载 作者:太空狗 更新时间:2023-10-29 16:00:04 24 4
gpt4 key购买 nike

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

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

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)
}

最佳答案

您可以使用 default arguments :

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

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

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

关于android - 隐藏 View 默认构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38440858/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com