Unresolved reference for synthetic view when layout is in library module

前端 未结 10 2109
醉梦人生
醉梦人生 2020-12-12 21:37

using Kotlin 1.20.20 (not that it matters, older versions behaves the same)

When layout is in separate library module Android Studio has no problem finding and refer

10条回答
  •  情深已故
    2020-12-12 21:37

    Preconditions

    Do not import the following library import kotlinx.android.synthetic.main.my_view.view.*

    app/MainActivity.kt

    override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            val view: View = MyView(this)
            view.findViewById(R.id.textViewPocLib).text = "I can edit the library components"
            setContentView(view)
    }
    

    my_library/MyView.kt

    class MyView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) :
        LinearLayout(context, attrs, defStyleAttr) {
    
        init {
            LayoutInflater.from(context)
                .inflate(R.layout.my_view, this, true)
        }
    }
    

    GL

    Sources:

    • Bug
    • Building Custom Component

提交回复
热议问题