在 xml 文件中删除未使用的项目(只是为了实现菜单主题)。所以,它会像:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:theme="@style/AppTheme" />
然后用于getMenu
添加新的菜单项,如下所示:
Button btn1= (Button) findViewById(R.id.btn_test);
PopupMenu popup = new PopupMenu(yourFormName.this, btn1);
//Inflating the Popup using xml file
popup.getMenu().add("Menu1 Label");
popup.getMenu().add("Menu2 Label");
popup.getMenuInflater().inflate(R.menu.YourXMLFileName, popup.getMenu());
//registering popup with OnMenuItemClickListener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
//---your menu item action goes here ....
Toast.makeText(DisplayTransactions.this,"You Clicked : " + item.getTitle(),Toast.LENGTH_SHORT).show();
return true;
}
});
popup.show();//showing popup menu