Kotlin Android 扩展和菜单

Kotlin Android Extensions and Menu

有没有办法使用合成属性来访问 fragment_photo_gallery 布局中定义的 menu_item_search 菜单项使用 findItem 方法?

override fun onCreateOptionsMenu(menu: Menu, menuInflater: MenuInflater) {
    super.onCreateOptionsMenu(menu, menuInflater)
    menuInflater.inflate(R.menu.fragment_photo_gallery, menu)

    //is there a way to access searchItem using synthetic properties?
    val searchItem = menu.findItem(R.id.menu_item_search)
}

MenuInflaterLayoutInflater 的用途完全不同。

尽管它们的名称中都有 "Inflater" 部分并实现了名为 "inflate()" 的方法,但它们做的事情完全不同。 MenuInflater 膨胀 Menus,其中 LayoutInflater 膨胀 Views。

Kotlin Android Extensions 的创建是为了简化 Android 视图的使用,而不是 Android 菜单或任何具有 inflate() 方法的东西。

长话短说 - 无法将 KAE 与 Android 菜单一起使用。