简体   繁体   English

如何在Android中以编程方式创建菜单?

[英]How to create a menu programmatically in Android?

Now I want to add it a menu on the bottom of the screen. 现在我想在屏幕底部添加一个菜单。 I wrote a lot of about but still didnt get how to do that. 我写了很多关于但仍然没有得到如何做到这一点。 My main problem is that I dont have an xml file on my main page. 我的主要问题是我的主页上没有xml文件。 its look liks that: 它看起来像是:

   public class start  extends ListActivity {
        static final String[] COUNTRIES = new String[] {
        "NEWS1", "NEWS2","RADIO"};
 Intent intent;
 public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, COUNTRIES));

      ListView lv = getListView();
      lv.setTextFilterEnabled(true);

      lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {
if (((TextView) view).getText().equals("NEWS1")){
 intent = new Intent(start.this,  NewsActivity.class);

how can I add a menu with an action. 如何添加带动作的菜单。 please give me a example. 请举个例子。 thanks 谢谢

use this code to add menu dynamically 使用此代码动态添加菜单

private static final int NEW_MENU_ID=Menu.FIRST+1;

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);

        menu.add(0, NEW_MENU_ID, 0, "New"); 

        return true;
    }

Ya its fine if you dont have an XML file in your main activity. 如果您的主要活动中没有XML文件,那就太好了。 You can still add menu to it by following this steps -> 您仍然可以按照以下步骤添加菜单 - >

  1. create an android XML file and select menu as type(It will get stored under res folder's menu sub folder). 创建一个Android XML文件并选择菜单作为类型(它将存储在res文件夹的菜单子文件夹下)。
  2. Now in your main activity code add this two methods -> 现在在您的主要活动代码中添加这两个方法 - >

     public boolean onCreateOptionsMenu(Menu menu){ getMenuInflater().inflate(R.menu.yourmenuxmlfilename, menu); return true; } public boolean onOptionsItemSelected(MenuItem item){ switch(item.getItemId){ case R.id.item1: // what you want to do with first button break; case ..... break; } return true; } 

For a context menu you just add 对于上下文菜单,您只需添加

            getListView().setOnCreateContextMenuListener(new OnCreateContextMenuListener() {

                @Override
                public void onCreateContextMenu(ContextMenu menu, View v,
                        ContextMenuInfo menuInfo) {

                    menu.setHeaderTitle("Options");
                    menu.add("Option1");
                    menu.add("Option2");
                    menu.add("Option3");

                }
            });

The context menu just appears on long click to the list item 只需长按一下即可显示上下文菜单

For options menu 对于选项菜单

create an android xml file that you inflate in options menu 创建一个在选项菜单中膨胀的android xml文件

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    new MenuInflater(this).inflate(R.layout.options, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    //and your action for menu item click goes here

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM