Kotlin Android扩展程序和菜单

Igo*_*jda 3 kotlin kotlin-android-extensions

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

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)
}
Run Code Online (Sandbox Code Playgroud)

小智 9

MenuInflater服务于一个根本不同的目的LayoutInflater.

尽管名称中包含"Inflater"部分并且实现名为"inflate()"的方法,但它们完全不同.MenuInflater膨胀Menus,LayoutInflater膨胀Views.

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

长话短说 - 不可能将KAE与Android菜单一起使用.