Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
466 views
in Technique[技术] by (71.8m points)

Android custom navigation view to accept menu resource

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...