zoukankan      html  css  js  c++  java
  • android menu 开发

    menu 分类:

    选项菜单(OptionsMenu
    上下文菜单(ContextMenu
    子菜单(SubMenu
    弹出菜单(Popup

     
    首先说
    选项菜单(OptionsMenu

    一、方法介绍:

           public booleanonCreateOptionsMenu(Menu menu):使用此方法调用OptionsMenu 。

           public booleanonOptionsItemSelected(MenuItem item):选中菜单项后发生的动作。

           public voidonOptionsMenuClosed(Menu menu):菜单关闭后发生的动作。

           public booleanonPrepareOptionsMenu(Menu menu):选项菜单显示之前onPrepareOptionsMenu方法会被调用,你可以用此方法来根据打当时的情况调整菜单。

          public booleanonMenuOpened(int featureId, Menu menu):单打开后发生的动作。

      二、默认样式

      默认样式是在屏幕底部弹出一个菜单,这个菜单我们就叫他选项菜单OptionsMenu,一般情况下,选项菜单最多显示2排每排3个菜单项,这些菜单项有文字有图标,也被称作Icon Menus,如果多于6项,从第六项开始会被隐藏,在第六项会出现一个More里,点击More才出现第六项以及以后的菜单项,这些菜单项也被称作Expanded Menus。下面介绍。

    1.重载onCreateOptionsMenu(Menu menu)方法
    	@Override
    	public boolean onCreateOptionsMenu(Menu menu)
    	{
    		// Inflate the menu; this adds items to the action bar if it is present.
    		//getMenuInflater().inflate(R.menu.main, menu);
    		menu.add(0,1,1,"设置").setIcon(R.drawable.setting);
    		menu.add(0,3,3,"设置").setIcon(R.drawable.setting);
    		menu.add(0,2,2,"下载").setIcon(R.drawable.download);
     
    	}

    重载onCreateOptionsMenu(Menu menu)方法,并在此方法中添加菜单项,最后返回true,如果false,菜单则不会显示。

    注意:
     大部分手机上边都会有一个“MENU”键,在一个应用安装到手机上之后,可以通过“MENU”显示该应用关联的菜单。
     但是,从Android 3.0开始,Android不再要求手机设备上必须提供MENU案件,虽然现在还有很多手机都会提供MENU按键,但是有一部分已经不再提供。在这种情况下,Android推荐使用ActionBar来代替菜单。在以后的博文中我们会介绍AndroidActionBar的支持

    4.2上的optionsMenu是放到actionbar上面了,必须要在xml文件中设置showAsAction="always"活着showAsAction="ifRoom"才能在actionbar上显示。只有在actionBar上显示的菜单,才会有图标。要在代码中设置的话,menu.findItem(id).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS)



    2.重载onOptionsItemSelected(MenuItem item)方法为捕捉菜单项事件

    	@Override
    	public boolean onOptionsItemSelected(MenuItem item)
    	{
    		// TODO Auto-generated method stub
    		if (item.getGroupId() == 0 &&item.getItemId() == 1)
    		{
    			Intent intent = new Intent(this, SecondActivity.class);
    			startActivity(intent);
    		}
    		else
    		{
    			Toast.makeText(this, item.getTitle(), Toast.LENGTH_SHORT).show();
    		}
    		return super.onOptionsItemSelected(item);
    	}


    上下文菜单(ContextMenu
    当用户长按一个控件时时,弹出的菜单我们称为上下文菜单。我们经常在Windows中用鼠标右键单击弹出的菜单就是上下文菜单。

    1重载ActivityonCreateContextMenu()方法,调用Menuadd方法添加菜单项MenuItem

    	@Override
    	public void onCreateContextMenu(ContextMenu menu, View v,
    			ContextMenuInfo menuInfo)
    	{
    		  menu.add(0, 1, 0, "红色背景");  
    		  menu.add(0, 2, 0, "绿色背景");  
    		  menu.add(1, 3, 0, "白色背景");  
    		// TODO Auto-generated method stub
    		super.onCreateContextMenu(menu, v, menuInfo);
    	}


    2重载onContextItemSelected()方法,响应菜单单击事件

    	@Override
    	public boolean onContextItemSelected(MenuItem item)
    	{
    		// TODO Auto-generated method stub
    		  switch(item.getItemId()) {  
    			  case 1:  
    				  myText.setBackgroundColor(Color.RED);  
    			   break;  
    			  case 2:  
    				  myText.setBackgroundColor(Color.GREEN);  
    			   break;  
    			  case 3:  
    				  myText.setBackgroundColor(Color.WHITE);  
    			   break;  
    			  } 
    		return true;
    	}


    3重载registerForContextMenu()方法,为视图注册上下文菜单

        private TextView myText;
    	@Override
    	protected void onCreate(Bundle savedInstanceState)
    	{
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    		myText = (TextView)findViewById(R.id.mytext);
    		registerForContextMenu(myText);  
    	}
    子菜单(SubMenu
     子菜单就是将相同功能的分组进行多级显示的一种菜单,比如,Windows的“文件”菜单中就有“新建”,“打开”,“关闭”等子菜单。

         创建子菜单的方法

    1重载ActivityonCreateOptionsMenu()方法,调用MenuaddSubMenu()方法添加子菜单项

    2、调用SubMenuadd()饭饭,添加子菜单项

    	@Override
    	public boolean onCreateOptionsMenu(Menu menu)
    	{
    		// Inflate the menu; this adds items to the action bar if it is present.
    
    		SubMenu subMenu = menu.addSubMenu(0, 4, 4, "登录/注册");
    		subMenu.add(1, 1, 1, "登录");
    		subMenu.add(1, 2, 2, "注册");
    		return true;
    	}


    3、重载onOptionsItemSelected(MenuItem item)方法为捕捉菜单项事件

    这个方法有一个MenuItem参数,可以使用其getTitle和getItemId方法来判断单击的是哪个菜单项
    	@Override
    	public boolean onOptionsItemSelected(MenuItem item) {
    		// TODO Auto-generated method stub
    		if(item.getTitle().equals("登录"))
    		{
    			Log.e("action:","点击了 注册/登录");
    		}
    		return super.onOptionsItemSelected(item);
    	}
    


     SubMenu是Menu的子接口,添加SubMenu后,可以通过SubMenu.add方法添加其子菜单项。在子菜单项上不能显示图像,但可以在子菜单的头部显示图像,不过子菜单项可以带复选框和选项按钮。
    注意:子菜单不能再增加子菜单;

    弹出菜单(Popup
       该类菜单需要和某个View绑定,当点击该View时在该View的上面或下面(视屏幕空间而定)弹出菜单。
    使用方法:
    1、建立XML菜单资源文件;
    popup.xml
    <menu xmlns:android="http://schemas.android.com/apk/res/android" >
    
        <item
            android:id="@+id/action_edit"
            android:orderInCategory="100"
            android:showAsAction="never"
            android:title="edit"/>
            <item
            android:id="@+id/action_share"
            android:orderInCategory="100"
            android:showAsAction="never"
            android:title="popup"/>
     
    
    </menu>


    2~5步均可在绑定View的点击事件中实现!
    2、建立PopupMenu对象,实例化传入上下文context和绑定的View;
    3、使用PopupMenu对象的getMenuInflater().inflate()把XML菜单文件压进来,
    4、使用PopupMenu对象自身的setOnMenuItemClickListener设置点击事件;
    5、使用PopupMenu对象自身的show显示弹出菜单。
    	public void showPopuMenu(View v)
    	{
    		PopupMenu popup = new PopupMenu(MainActivity.this, v);
    	    MenuInflater inflater = popup.getMenuInflater();
    	    inflater.inflate(R.menu.popup, popup.getMenu());
    	    popup.show();
    
    	}

    demo:http://download.csdn.net/detail/q610098308/9236383


  • 相关阅读:
    win10系统u盘安装单个文件超过4g解决办法
    单片机下使用IIC
    uart
    socket
    Linux中 ./configure --prefix命令
    linux下配置安装python3
    linux下的dhcp服务器实现
    安卓出现错误: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
    C语言基础02
    C语言基础01
  • 原文地址:https://www.cnblogs.com/sharecenter/p/5621037.html
Copyright © 2011-2022 走看看