Warm tip: This article is reproduced from stackoverflow.com, please click
android android-custom-view android-viewbinding

How to use View Binding on custom views

发布于 2020-05-12 13:46:31

View Binding got released with v3.6.

Docs: https://developer.android.com/topic/libraries/view-binding

My question is, how to use view binding with custom views. Google documentation has only show-cased Activity and fragment.

I tried this, but nothing was shown.

LayoutInflater inflater = LayoutInflater.from(getContext());

And then, I used this one, but again, no luck.

LayoutInflater inflater = (LayoutInflater)
            getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

I guess, maybe I don't target the correct layout inflater for my view but not sure about it.

Questioner
Emi Raz
Viewed
14
Androiderson 2020-04-14 06:11

Just inform the root, and whether you want to attach to it

init { // inflate binding and add as view
    binding = ResultProfileBinding.inflate(LayoutInflater.from(context), this, true)
}