Android(一) 动态菜单

1.android的一个activity可以再选中某项之后按menu键弹出特定的菜单,也就是动态菜单。动态菜单的实现是靠menu类中的addIntentOptions函数实现的,具体的声明如下:

int android.view.Menu.addIntentOptions(
  int groupId,
  int itemId,
  int order,
  ComponentName caller,
  Intent[] specifics,                ------------ action+uri的具体方式来增加激活相应activity的菜单项
  Intent intent,        ---------- categroy+uri这种一般形式来增加激活相应activity的菜单项
  int flags,
  MenuItem[] outSpecificItems)  ---------Optional array in which to place the menu items that were generated for each of the specifics that were requested.

2.notepad源码示例

           Uri uri = ContentUris.withAppendedId(getIntent().getData(), getSelectedItemId());


            Intent[] specifics = new Intent[1];
            specifics[0] = new Intent(Intent.ACTION_EDIT, uri);
            MenuItem[] items = new MenuItem[1];


            Intent intent = new Intent(null, uri);
            intent.addCategory(Intent.CATEGORY_ALTERNATIVE);
            menu.addIntentOptions(Menu.CATEGORY_ALTERNATIVE, 0, 0, null, specifics, intent, 0, items);

3.自己的测试代码:在notepad项目中增加一个activity并注册

        <activity android:name="MyAdd" android:label="@string/title_myadd"    -------菜单项显示的名称
                android:windowSoftInputMode="stateVisible">
            <intent-filter android:label="@string/resolve_myadd">
                <action android:name="com.android.notepad.action.MYADD" />
                <category android:name="android.intent.category.ALTERNATIVE" />
                <data android:mimeType="vnd.android.cursor.item/vnd.google.note" />
            </intent-filter>
        </activity>
原文地址:https://www.cnblogs.com/yuyutianxia/p/3291128.html