[英]How to use Android ViewBinding for custom view which inflates from existing layout
I see there is ViewBinding support for Activity and Fragment.我看到有对 Activity 和 Fragment 的 ViewBinding 支持。 But how about custom view?但是自定义视图呢?
My custom view likes this:我的自定义视图喜欢这样:
class MyView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0,
defStyleRes: Int = 0
) : LinearLayout(context, attrs, defStyleAttr, defStyleRes) {
init {
inflate(context, R.layout.my_view, this)
}
}
I try to create ViewBinding but it doesn't work.我尝试创建 ViewBinding 但它不起作用。
private val binding: MyViewBinding
init {
inflate(context, R.layout.my_view, this)
binding = MyViewBinding.inflate(LayoutInflater.from(context))
}
Do you have any solution for ViewBinding with custom view?你有自定义视图的 ViewBinding 解决方案吗?
you try replace你尝试替换
init {
inflate(context, R.layout.my_view, this)
binding = MyViewBinding.inflate(LayoutInflater.from(context))
}
with和
init {
binding = MyViewBinding.inflate(LayoutInflater.from(context),this, false)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.