Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
287 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 WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...