Use of the Android PopupMenu pop-up menu

Source: Internet
Author: User



PopupMenu represents a pop-up menu. He will specify a popup popupmenu on the component, by default PopupMenu will appear below or above the component, PopupMenu can add multiple menu items and can be added to the menu item submenu,

First write the XML file under Res/menu My_menu.xml

<menu xmlns:android= "Http://schemas.android.com/apk/res/android" >

<item
android:icon= "@drawable/ic_launcher"
android:title= "Fonts" >
<menu>

<!--define a single menu item--
<group android:checkablebehavior= "single" >
<item
Android:id= "@+id/font_10"
Android:title= "Font No. 10th"/>
<item
Android:id= "@+id/font_12"
Android:title= "Font No. 12th"/>
<item
Android:id= "@+id/font_14"
Android:title= "Font No. 14th"/>
<item
Android:id= "@+id/font_16"
Android:title= "Font No. 16th"/>
<item
Android:id= "@+id/font_18"
Android:title= "Font No. 18th"/>
</group>
</menu>
</item>
<!--define a common menu item--
<item
android:icon= "@drawable/FY_D_KF"
android:title= "normal Menu"/>
<item
android:icon= "@drawable/fy_d_kf2"
android:title= "COLOR" >
<menu>

<!--define a single menu item--
<group>
<item
Android:id= "@+id/red"
android:title= "Red"/>
<item
Android:id= "@+id/blue"
android:title= "Blue"/>
<item
Android:id= "@+id/green"
android:title= "Green"/>
</group>
</menu>
</item>

</menu>

The main code in the activity is as follows

public class Mainactivity extends Activity {

Private TextView edit;
Private PopupMenu menu;

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Edit = (TextView) Findviewbyid (R.ID.TEXTVIEW1);
Edit.setonclicklistener (New Onclicklistener () {

@Override
public void OnClick (View arg0) {
Clickpopumne (arg0);
}
});
}

public void Clickpopumne (View v) {
menu = new PopupMenu (this, v);
Getmenuinflater (). Inflate (R.menu.my_menu, Menu.getmenu ());
Menu.setonmenuitemclicklistener (New Onmenuitemclicklistener () {

@Override
public boolean Onmenuitemclick (MenuItem arg0) {
Switch (Arg0.getitemid ()) {
Case R.ID.FONT_10:
Menu.dismiss ();
Break
Default
Toast.maketext (Mainactivity.this, "" +arg0.gettitle (), Toast.length_short). Show ();
}
return true;
}
});
Menu.show ();
}
}


Use of the Android PopupMenu pop-up menu

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.