Use of PopupMenu in Android

Source: Internet
Author: User

PopupMenu can be very convenient to display a pop-up menu below the specified view, similar to the effect of the Actionbar overflow menu. Its menu options are available from the menu resource and are therefore very convenient. Here is a picture of a popupmenu running on the internet.

How to use

New Popup_menu.xml under Res/menu

1234567891011 <menu xmlns:android="http://schemas.android.com/apk/res/android"      xmlns:tools="http://schemas.android.com/tools">  <item     android:id="@+id/special_topic"     android:title="专题"/>      <item          android:id="@+id/elite"        android:title="精选"/>    </menu>

Use PopupMenu in activity or fragment

1234567891011121314151617181920        final  ImageButton moreMenu = (ImageButton)v.findViewById(R.id.more_menu);        moreMenu.setOnClickListener(newOnClickListener() {            @Override            public void onClick(View v){                   PopupMenu popup = newPopupMenu(getActivity(), moreMenu);                    //Inflating the Popup using xml file                    popup.getMenuInflater()                        .inflate(R.menu.poupup_menu_home, popup.getMenu());                    //registering popup with OnMenuItemClickListener                    popup.setOnMenuItemClickListener(newPopupMenu.OnMenuItemClickListener() {                        public boolean onMenuItemClick(MenuItem item) {                                              return true;                        }                    });                    popup.show(); //showing popup menu             }        });

Where Moremenu is a button that is triggered by his popupmenu display.

Use of PopupMenu in Android

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.