【问题标题】:Call requires API level 21 (current min is 19): android.view.View()调用需要 API 级别 21(当前最低为 19):android.view.View()
【发布时间】:2020-10-07 02:58:21
【问题描述】:

所以我想创建一个使用自定义视图的库。我想继承 View 类,但是因为我的 minsdk 是 19 是错误的。我需要我的库来支持 sdk 19。有没有办法解决这个问题?

class Dummy() :  View(context, attrs, defStyleAttr, defStyleRes) {

【问题讨论】:

    标签: android view sdk android-custom-view


    【解决方案1】:

    您应该为此使用另一个构造函数。您使用的那个只能从 API 21 获得。

    对于自定义视图,我这样使用它:

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

    这里有两件事:

    1. 使用带有 3 个参数的 View 构造函数。上下文、属性、defStyleAttr。
    2. 使用@JvmOverloads 注解和参数默认值,实际上支持一行3个构造函数。

    【讨论】:

    • 它有效,我爱你。但是使用这个构造函数有缺点吗?是否有不受支持的功能或其他?
    • 没有缺点。 defStyleRes 仅用于在 defStyleAttr == 0 时应用默认样式。您可能永远不需要它们。即使您需要支持类似的东西,您也可以将其添加到您的自定义视图构造函数中并修改 init-block 逻辑。
    猜你喜欢
    • 2017-02-23
    • 2021-01-13
    • 1970-01-01
    • 2015-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多