我应该如何在对话框中添加一些操作项?

时间:2013-04-05 17:18:40

标签: android android-actionbar android-dialog android-menu android-optionsmenu

我有一个自定义对话框,我想添加一些带有图标的动作项,如android calender。

android calender with dialog with action items

这里是为我的活动提供主题的清单代码:

    <activity
        android:name=".FileChooser"
        android:label="FileChooser"
        android:theme="@android:style/Theme.DeviceDefault.Dialog"  tools:ignore="NewApi">
    </activity>

并在FileChooser.java中:

     public boolean onCreateOptionsMenu(Menu menu) 
 {
        MenuItem camramnu= menu.add(0,0,0,"Take a picture");
        {
            camramnu.setIcon(R.drawable.camera);
            camramnu.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
        }
        return true;
 }

它永远不会显示图标!

我该怎么办?

2 个答案:

答案 0 :(得分:1)

我看到了问题 - 你有一个选项菜单,但是从你所包含的图片中已经看起来像一个上下文操作栏菜单正在运行,有“完成”和“取消”

也许您需要在

上设置菜单选项

http://developer.android.com/guide/topics/ui/menus.html#CAB

在那种情况下,

private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {

// Called when the action mode is created; startActionMode() was called
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
    // Inflate a menu resource providing context menu items
    MenuInflater inflater = mode.getMenuInflater();
    inflater.inflate(R.menu.context_menu, menu);
    return true;
}

// Called each time the action mode is shown. Always called after onCreateActionMode, but
// may be called multiple times if the mode is invalidated.
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
    return false; // Return false if nothing is done
}

// Called when the user selects a contextual menu item
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
    switch (item.getItemId()) {
        case R.id.menu_share:
            shareCurrentItem();
            mode.finish(); // Action picked, so close the CAB
            return true;
        default:
            return false;
    }
}

// Called when the user exits the action mode
@Override
public void onDestroyActionMode(ActionMode mode) {
    mActionMode = null;
}

};

答案 1 :(得分:0)

是片段中的菜单吗?在你必须使用的片段中     setHasOptionsMenu(真);