当前位置:操作系统 > 安卓/Android >>

从头学Android之SubMenu子菜单

子菜单
子菜单就是将相同功能的分组进行多级显示的一种菜单。
创建子菜单的步骤:
1、  覆盖Activity的onCreateOptionsMenu()方法,调用Menu的addSubMenu()方法来添加子菜单
2、  调用SubMenu的add()方法,添加子菜单
3、  覆盖onContextItemSelected()方法,响应子菜单的单击事件
 
至于onCreateOptionsMenu()方法在前面已做过介绍,现在来看下Menu的addSubMenu()方法,与add()方法一样它有四个重载方法,参数也同出一辙。在这也就不再多说明了
public abstract SubMenu addSubMenu (int groupId, int itemId, int order, CharSequence title)
 
public abstract SubMenu addSubMenu (int groupId, int itemId, int order, int titleRes)
 
public abstract SubMenu addSubMenu (CharSequence title)
 
public abstract SubMenu addSubMenu (int titleRes)
这四个函数的返回对象都是SubMenu,那么我们就来看下SubMenu吧,随便来了解它的一些属性及方法:
SubMenu是实现Menu的
比较常用的方法有:setHeaderIcon()、setHeaderTitle()
下面通过两个实例来熟悉下SubMenu
 
实例1:

package com.jiahui.activity; 
 
import java.lang.reflect.Field; 
 
import android.app.Activity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.SubMenu; 
 
public class SubMenuDemoActivity extends Activity { 
 
    private static final int ITEM1 = Menu.FIRST; 
    private static final int ITEM2 = Menu.FIRST + 1; 
 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
    } 
 
    public boolean onCreateOptionsMenu(Menu menu) { 
 
        SubMenu file = menu.addSubMenu("文件"); 
        SubMenu edit = menu.addSubMenu("编辑"); 
         
        file.add(0, ITEM1, 0, "新建"); 
        file.add(0, ITEM2, 0, "打开"); 
        return true; 
    } 
 
    public boolean onOptionsItemSelected(MenuItem item) { 
        switch (item.getItemId()) { 
        case ITEM1: 
            setTitle("新建文件"); 
            break; 
 
        case ITEM2: 
            setTitle("打开文件"); 
            break; 
        } 
 
        return true; 
    } 
 

效果:

\

\


实例2:

package com.jiahui.activity; 
 
import android.app.Activity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.SubMenu; 
 
public class SubMenuDemo_2Activity extends Activity { 
    
    private static final int UPLOAD=Menu.FIRST; 
     
    private static final int DOWNLOAD=Menu.FIRST+1; 
     
     
    private static final int SUB_UPLOAD_A=Menu.FIRST+2; 
    private static final int SUB_UPLOAD_B=Menu.FIRST+3; 
     
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
    } 
 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
         
         
        SubMenu uploadSubMenu=menu.addSubMenu(0, UPLOAD, 0, "上传");   
        uploadSubMenu.setIcon(R.drawable.upload); 
         
        uploadSubMenu.setHeaderIcon(R.drawable.upload);//设置子菜单的标题 
        uploadSubMenu.setHeaderTitle("上传参数设置");//设置子菜单的标题  默认为“上传” 
        uploadSubMenu.add(0, SUB_UPLOAD_A, 0, "上传参数1"); 
        uploadSubMenu.add(0, SUB_UPLOAD_B, 0, "上传参数2"); 
         
         
        SubMenu downloadSubMenu=menu.addSubMenu(0, DOWNLOAD, 0, "下载"); 
        downloadSubMenu.setIcon(R.drawable.download); 
         
        return super.onCreateOptionsMenu(menu); 
    } 
     
     
     


 
 
效果:
 

\

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,