예제 #1
0
 public PopupMenu(Context context, View anchor) {
   mContext = context;
   mMenu = new MenuBuilder(context);
   mMenu.setCallback(this);
   mAnchor = anchor;
   mPopup = new PopupMenuHelper(context, mMenu, mAnchor);
   mPopup.setCallback(this);
 }
  @Override
  public void dispatchInvalidateOptionsMenu() {
    if (DEBUG) Log.d(TAG, "[dispatchInvalidateOptionsMenu]");

    if (mMenu == null) {
      Context context = mActivity;
      if (mActionBar != null) {
        TypedValue outValue = new TypedValue();
        mActivity.getTheme().resolveAttribute(R.attr.actionBarWidgetTheme, outValue, true);
        if (outValue.resourceId != 0) {
          // We are unable to test if this is the same as our current theme
          // so we just wrap it and hope that if the attribute was specified
          // then the user is intentionally specifying an alternate theme.
          context = new ContextThemeWrapper(context, outValue.resourceId);
        }
      }
      mMenu = new MenuBuilder(context);
      mMenu.setCallback(mMenuBuilderCallback);
    }

    mMenu.stopDispatchingItemsChanged();
    mMenu.clear();

    if (!callbackCreateOptionsMenu(mMenu)) {
      mMenu = null;
      if (mActionBar != null) {
        if (DEBUG) Log.d(TAG, "[dispatchInvalidateOptionsMenu] setting action bar menu to null");
        mActionBar.setMenu(null, mMenuPresenterCallback);
      }
      return;
    }

    if (!callbackPrepareOptionsMenu(mMenu)) {
      if (mActionBar != null) {
        if (DEBUG) Log.d(TAG, "[dispatchInvalidateOptionsMenu] setting action bar menu to null");
        mActionBar.setMenu(null, mMenuPresenterCallback);
      }
      mMenu.startDispatchingItemsChanged();
      return;
    }

    // TODO figure out KeyEvent? See PhoneWindow#preparePanel
    KeyCharacterMap kmap = KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD);
    mMenu.setQwertyMode(kmap.getKeyboardType() != KeyCharacterMap.NUMERIC);
    mMenu.startDispatchingItemsChanged();

    if (DEBUG) Log.d(TAG, "[dispatchInvalidateOptionsMenu] setting action bar menu to " + mMenu);
    mActionBar.setMenu(mMenu, mMenuPresenterCallback);
  }
예제 #3
0
  private boolean initializePanelMenu() {
    Context context = mActivity; // getContext();

    // If we have an action bar, initialize the menu with a context themed for it.
    if (wActionBar != null) {
      TypedValue outValue = new TypedValue();
      Resources.Theme currentTheme = context.getTheme();
      currentTheme.resolveAttribute(R.attr.actionBarWidgetTheme, outValue, true);
      final int targetThemeRes = outValue.resourceId;

      if (targetThemeRes != 0 /*&& context.getThemeResId() != targetThemeRes*/) {
        context = new ContextThemeWrapper(context, targetThemeRes);
      }
    }

    mMenu = new MenuBuilder(context);
    mMenu.setCallback(this);

    return true;
  }