Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android中无XML文件填充弹出菜单_Android_Xml_Menu_Popupmenu - Fatal编程技术网

android中无XML文件填充弹出菜单

android中无XML文件填充弹出菜单,android,xml,menu,popupmenu,Android,Xml,Menu,Popupmenu,我有这个代码,这个工作很完美。只有我想在不使用xml文件(actions.xml)的情况下使其成为动态的。我该怎么做 public void showPopup(View v) { PopupMenu popup = new PopupMenu(this, v); MenuInflater inflater = popup.getMenuInflater(); inflater.inflate(R.menu.actions, popup.getMenu()); p

我有这个代码,这个工作很完美。只有我想在不使用xml文件(actions.xml)的情况下使其成为动态的。我该怎么做

public void showPopup(View v) {
    PopupMenu popup = new PopupMenu(this, v);
    MenuInflater inflater = popup.getMenuInflater();
    inflater.inflate(R.menu.actions, popup.getMenu());
    popup.show();
}

使用
popup.getMenu()
,然后使用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" />

不错的tnx!:)你知道如何设置弹出菜单的样式吗?我不知道如何具体设置,不知道。你可以看看。如何获得选定值?
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