I'm implementing a custom navigation view where I want to accept a menu resource to describe menu items. As you can see in code snippet below, I'm using a workaround to get a menu and inflate the menu resource
init {
val attributes = context.theme.obtainStyledAttributes(attrs, R.styleable.TabNavigationView, 0, 0)
try {
val navigationItemsResource = attributes.getResourceId(R.styleable.TabNavigationView_menu_items, NO_MENU)
// A work-around to a get a Menu instance
val popupMenu = PopupMenu(context, null)
val menu = popupMenu.menu
MenuInflater(context).inflate(navigationItemsResource, menu)
setNavigationMenu(menu)
} finally {
attributes.recycle()
}
}
Is there a regular/not hacky/official way to inflate the menu defined in xml in my custom view?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…