简体   繁体   English

如何在 android (Kotlin) 中扩展 Snackbar

[英]How to extend Snackbar in android (Kotlin)

I want to create a class which extends Snackbar but I couldn't find the constructor in the Google documentation.我想创建一个扩展 Snackbar 的类,但在 Google 文档中找不到构造函数。 How should I add the constructor to the class:我应该如何将构造函数添加到类中:

import com.google.android.material.snackbar.Snackbar

class MySnackbar : Snackbar { 
    // Error: need a constructor
}

SnackBar is final class and thus cannot be inherited. SnackBar 是 final 类,因此不能被继承。 One way is to access SnackBar's view ( snackbar.getView() ) and modify the view as per your need.一种方法是访问 SnackBar 的视图( snackbar.getView() )并根据需要修改视图。

Snakbar 是 final 类,它不是继承的,但您可以修改 Snakbar 的视图。

You can create secondary constructor, Use below code for Kotlin.您可以创建辅助构造函数,使用下面的 Kotlin 代码。

class MyCustomSeekBar : AppCompatSeekBar {
    constructor(context: Context?) : super(context)
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
}

Hope it helps you.希望对你有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM