![](/img/trans.png)
[英]What is the correct approach / logic to access the activity which inflates a layout with custom view from this view?
[英]How to use Android ViewBinding for custom view which inflates from existing layout
我看到有對 Activity 和 Fragment 的 ViewBinding 支持。 但是自定義視圖呢?
我的自定義視圖喜歡這樣:
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)
}
}
我嘗試創建 ViewBinding 但它不起作用。
private val binding: MyViewBinding
init {
inflate(context, R.layout.my_view, this)
binding = MyViewBinding.inflate(LayoutInflater.from(context))
}
你有自定義視圖的 ViewBinding 解決方案嗎?
你嘗試替換
init {
inflate(context, R.layout.my_view, this)
binding = MyViewBinding.inflate(LayoutInflater.from(context))
}
和
init {
binding = MyViewBinding.inflate(LayoutInflater.from(context),this, false)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.