Add gravity settings to PopupWindow/ListPopupWindow/PopupMenu

Allow calling code to specify left/right/start/end gravity when
showing a popup attached to an anchor. This allows easy alignment of
either the right or left edges of the popup and anchor view.

Bug 10728401

Change-Id: Ie0844a04ea0576fa67b0972f5873aaa4c5b823f6
This commit is contained in:
Adam Powell
2013-09-26 15:36:34 -07:00
parent bde988ade5
commit 54c94dea8a
6 changed files with 98 additions and 17 deletions

View File

@@ -25,6 +25,7 @@ import android.transition.Transition;
import android.transition.TransitionManager;
import android.util.SparseBooleanArray;
import android.view.ActionProvider;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.SoundEffectConstants;
import android.view.View;
@@ -665,6 +666,7 @@ public class ActionMenuPresenter extends BaseMenuPresenter
public OverflowPopup(Context context, MenuBuilder menu, View anchorView,
boolean overflowOnly) {
super(context, menu, anchorView, overflowOnly);
setGravity(Gravity.END);
setCallback(mPopupPresenterCallback);
}

View File

@@ -19,6 +19,7 @@ package com.android.internal.view.menu;
import android.content.Context;
import android.content.res.Resources;
import android.os.Parcelable;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MenuItem;
@@ -69,6 +70,8 @@ public class MenuPopupHelper implements AdapterView.OnItemClickListener, View.On
/** Cached content width from {@link #measureContentWidth}. */
private int mContentWidth;
private int mDropDownGravity = Gravity.NO_GRAVITY;
public MenuPopupHelper(Context context, MenuBuilder menu) {
this(context, menu, null, false);
}
@@ -102,6 +105,10 @@ public class MenuPopupHelper implements AdapterView.OnItemClickListener, View.On
mForceShowIcon = forceShow;
}
public void setGravity(int gravity) {
mDropDownGravity = gravity;
}
public void show() {
if (!tryShow()) {
throw new IllegalStateException("MenuPopupHelper cannot be used without an anchor");
@@ -126,6 +133,7 @@ public class MenuPopupHelper implements AdapterView.OnItemClickListener, View.On
if (addGlobalListener) mTreeObserver.addOnGlobalLayoutListener(this);
anchor.addOnAttachStateChangeListener(this);
mPopup.setAnchorView(anchor);
mPopup.setDropDownGravity(mDropDownGravity);
} else {
return false;
}