下面列出了android.widget.PopupWindow#showAtLocation ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public BigImgView(final Context context, String url, final View view) {
// 一个自定义的布局,作为显示的内容
final View contentView = LayoutInflater.from(context).inflate(
R.layout.vw_bigimg, null);
popupWindow = new PopupWindow(contentView,
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
popupWindow.setOutsideTouchable(true);
popupWindow.setTouchable(true);
ImageView imageView = (ImageView) contentView.findViewById(R.id.bigImg_img);
ViewUtils.showImage(imageView, url);
Logger.i("显示图片:"+url);
imageView.setOnClickListener(this);
contentView.findViewById(R.id.bigImgPop_layout).setOnClickListener(this);
// 如果不设置PopupWindow的背景,无论是点击外部区域还是Back键都无法dismiss弹框
// 我觉得这里是API的一个bug
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.showAtLocation(view, Gravity.NO_GRAVITY, 0, 0);
}
/**
* 弹出popupWindow
* Show popup.
*
* @param gravity the gravity
* @param width the width
* @param height the height
* @param animationStyle the animation style
*/
private void showPopup(int gravity, int width, int height, int animationStyle) {
mPopupView = LayoutInflater.from(mActivity).inflate(R.layout.popup_demo, null);
mPopupWindow = new PopupWindow(mPopupView, width, height);
//以下属性响应空白处消失和实体按键返回消失popup
mPopupWindow.setOutsideTouchable(true);
mPopupWindow.setFocusable(true);
mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
//沉浸式模式下,以下两个属性并不起作用
mPopupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
mPopupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
//重点,此方法可以让布局延伸到状态栏和导航栏
mPopupWindow.setClippingEnabled(false);
//设置动画
mPopupWindow.setAnimationStyle(animationStyle);
//弹出
mPopupWindow.showAtLocation(getWindow().getDecorView(), gravity, 0, 0);
//弹出后背景alpha值
backgroundAlpha(0.5f);
//消失后恢复背景alpha值
mPopupWindow.setOnDismissListener(() -> backgroundAlpha(1f));
//适配弹出popup后布局被状态栏和导航栏遮挡问题
updatePopupView();
}
/**
* 长按弹出菜单
*
* @param offsetY
* @param actionMenu
* @return 菜单创建成功,返回true
*/
private void showActionMenu(int offsetY, ActionMenu actionMenu) {
mActionMenuPopupWindow = new PopupWindow(actionMenu, WindowManager.LayoutParams.WRAP_CONTENT,
mActionMenuHeight, true);
mActionMenuPopupWindow.setFocusable(true);
mActionMenuPopupWindow.setOutsideTouchable(false);
mActionMenuPopupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));
mActionMenuPopupWindow.showAtLocation(this, Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, offsetY);
mActionMenuPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
// 清理已选的文字
clearSelectedTextBackground();
}
});
}
/**
* 删除或重发窗体显示
* @author leibing
* @createTime 2017/5/5
* @lastModify 2017/5/5
* @param v 位置参照物
* @param action 操作名称
* @param onClick 点击事件
* @return
*/
private void showPopUp(View v,String action, View.OnClickListener onClick){
LinearLayout layout = new LinearLayout(this);
layout.setBackground(getResources().getDrawable(R.drawable.jk_chat_long_press_the_trigger));
layout.setGravity(Gravity.CENTER);
TextView tv = new TextView(this);
tv.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
tv.setText(action);
tv.setPadding(0, 0, 0, 16);
tv.setTextColor(Color.WHITE);
tv.setOnClickListener(onClick);
layout.addView(tv);
popupWindow = new PopupWindow(layout, 120, 100);
popupWindow.setFocusable(true);
popupWindow.setOutsideTouchable(true);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
int[] location = new int[2];
v.getLocationOnScreen(location);
popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0] + v.getMeasuredWidth() / 3,
location[1]-popupWindow.getHeight());
}
private void layoutAndShowPopupWindow(Key key, int xPosition) {
popupWindow = new PopupWindow(popupView,
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
popupWindow.setClippingEnabled(false);
int[] location = new int[2];
key.getLocationInWindow(location);
int measureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
popupView.measure(measureSpec, measureSpec);
int popupWidth = popupView.getMeasuredWidth();
int spaceAboveKey = key.getHeight() / 4;
int x = xPosition - popupWidth / popupView.getChildCount() / 2;
int screenWidth = getScreenWidth();
if (x < 0) {
x = 0;
} else if (x + popupWidth > screenWidth) {
x = screenWidth - popupWidth;
}
int y = location[1] - popupView.getMeasuredHeight() - spaceAboveKey;
popupWindow.showAtLocation(key, Gravity.NO_GRAVITY, x, y);
//popupWindow.showAsDropDown(key, 0, -500);
}
public void show(@NonNull final EmojiImageView clickedImage, @NonNull final Emoji emoji) {
dismiss();
rootImageView = clickedImage;
final View content = initView(clickedImage.getContext(), emoji, clickedImage.getWidth());
popupWindow = new PopupWindow(content, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
popupWindow.setFocusable(true);
popupWindow.setOutsideTouchable(true);
popupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NOT_NEEDED);
popupWindow.setBackgroundDrawable(new BitmapDrawable(clickedImage.getContext().getResources(), (Bitmap) null));
content.measure(makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
final Point location = Utils.locationOnScreen(clickedImage);
final Point desiredLocation = new Point(
location.x - content.getMeasuredWidth() / 2 + clickedImage.getWidth() / 2,
location.y - content.getMeasuredHeight()
);
popupWindow.showAtLocation(rootView, Gravity.NO_GRAVITY, desiredLocation.x, desiredLocation.y);
rootImageView.getParent().requestDisallowInterceptTouchEvent(true);
Utils.fixPopupLocation(popupWindow, desiredLocation);
}
@SuppressWarnings("deprecation")
public void progressPopUp(final Activity context,String data){
RelativeLayout layoutId = (RelativeLayout)context.findViewById(R.id.dialog_rootView);
LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.progresspopup, layoutId);
txtWaiting = (TextView)layout.findViewById(R.id.txtWaiting);
paymentAlert = new PopupWindow(context);
paymentAlert.setContentView(layout);
paymentAlert.setHeight(WindowManager.LayoutParams.MATCH_PARENT);
paymentAlert.setWidth(WindowManager.LayoutParams.MATCH_PARENT);
paymentAlert.setFocusable(true);
paymentAlert.setBackgroundDrawable(new BitmapDrawable());
paymentAlert.showAtLocation(layout, Gravity.CENTER, 0, 0);
if(data !=null)
txtWaiting.setText(data);
}
public void showDescription() {
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
@SuppressLint("InflateParams") View layout = inflater.inflate(R.layout.popup_description, null);
int height = (int)(this.height * 0.8f);
int width = (int)(this.width * 0.8f);
final PopupWindow descriptionPopup = new FListPopupWindow(layout, width, height);
descriptionPopup.showAtLocation(chatFragment.getView(), Gravity.CENTER, 0, 0);
final TextView descriptionText = (TextView)layout.findViewById(R.id.descriptionText);
descriptionText.setText(SmileyReader.addSmileys(this, chatroomManager.getActiveChat().getDescription()));
// Enable touching/clicking links in text
descriptionText.setMovementMethod(LinkMovementMethod.getInstance());
}
/**
* 添加修改重复
*/
private void showRepeat() {
ListView v = new ListView(context);
v.setFooterDividersEnabled(false);
final PopupWindow popupWindow = new PopupWindow(v, PixelUtil.dp2px(100), PixelUtil.dp2px(180));
popupWindow.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.card_bg));
popupWindow.setFocusable(true);
popupWindow.setOutsideTouchable(true); // 点击popWin
popupWindow.setOutsideTouchable(true); // 以处的区域,自动关闭
int[] location = new int[2];
tv_repeat.getLocationOnScreen(location);
popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0], location[1] + tv_repeat.getHeight());
final List<String> list = new ArrayList<>();
list.add("单次");
list.add("每天");
list.add("每周");
list.add("每月");
v.setAdapter(new ArrayAdapter<String>(context, R.layout.item_list_simple, list));
v.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
task.setRepeat(position);
tv_repeat.setText(list.get(position));
popupWindow.dismiss();
}
});
}
public static void showWxShareDialog(Context context, View parent, Article article) {
PopupWindow popupWindow = new PopupWindow(LayoutInflater.from(context).inflate(R.layout.wechat_share, null), ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
View.OnClickListener listener = (View v) -> {
switch (v.getId()) {
case R.id.share_friends:
WxShareUtils.getInstance().shareToWeChat(context, article.getLink(), article.getTitle(), article.getDesc(), 0);
break;
case R.id.share_timeline:
WxShareUtils.getInstance().shareToWeChat(context, article.getLink(), article.getTitle(), article.getDesc(), 1);
break;
default:
Log.e("WxShareDialg", "no this view" + v.getId());
}
popupWindow.dismiss();
};
popupWindow.setFocusable(true);
popupWindow.setOutsideTouchable(true);
popupWindow.setBackgroundDrawable(context.getDrawable(R.drawable.wx_share_bg));
popupWindow.getContentView().findViewById(R.id.share_friends).setOnClickListener(listener);
popupWindow.getContentView().findViewById(R.id.share_timeline).setOnClickListener(listener);
popupWindow.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
}
@OnClick(R.id.mButton3)
public void onClick3() {
View dialogView = getLayoutInflater().inflate(R.layout.ui_dialog, null);
final PopupWindow popupWindow = new PopupWindow(dialogView,
(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 250,
getResources().getDisplayMetrics()),
(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 186,
getResources().getDisplayMetrics()), true);
popupWindow.setOutsideTouchable(true);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.showAtLocation(mContentHolder, Gravity.CENTER, 0, 0);
}
private void showPopupWindowList(PopupWindow popupWindow, View view) {
popupWindow.setOutsideTouchable(true);
popupWindow.setWidth(getResources().getDimensionPixelSize(R.dimen.popup_width));
popupWindow.setHeight(getResources().getDimensionPixelSize(R.dimen.popup_width));
int[] location = new int[2];
view.getLocationInWindow(location);
int x = location[0] - popupWindow.getWidth();
int y = location[1] + view.getHeight() / 2 - popupWindow.getHeight() / 2;
popupWindow.showAtLocation(view, Gravity.NO_GRAVITY, x, y);
}
/**
* 从Activity底部弹出来
*/
public void showAtBottom(Activity activity, View parent, ImageFolderBean curFloder, onFloderItemClickListener listener)
{
this.mActReference = new WeakReference<>(activity);
this.mListener = listener;
View contentView = LayoutInflater.from(activity).inflate(R.layout.layout_image_floder_pop, null);
int height = activity.getResources().getDimensionPixelSize(R.dimen.imagepicker_floder_pop_height);
mPopupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.MATCH_PARENT, height, true);
mPopupWindow.setBackgroundDrawable(new BitmapDrawable());// 响应返回键必须的语句。
mPopupWindow.setFocusable(true);//设置pop可获取焦点
mPopupWindow.setAnimationStyle(R.style.FloderPopAnimStyle);//设置显示、消失动画
mPopupWindow.setOutsideTouchable(true);//设置点击外部可关闭pop
mPopupWindow.setOnDismissListener(this);
mListView = (ListView) contentView.findViewById(R.id.lv_image_floder_pop);
final int position = ImageDataModel.getInstance().getAllFolderList().indexOf(curFloder);
mAdapter = new ImageFloderAdapter(activity, position);
mListView.setAdapter(mAdapter);
mListView.setOnItemClickListener(this);
mPopupWindow.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
toggleWindowAlpha();
// 增加绘制监听
ViewTreeObserver vto = mListView.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener()
{
@Override
public void onGlobalLayout()
{
// 移除监听
mListView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
mListView.setSelection(position);
}
});
}
private void touchLong() {
LogManager.getLogger().d(Tag, "touchLong");
View gifView = LayoutInflater.from(getContext()).inflate(R.layout.view_popupimg, null);
GifView gif = (GifView) gifView.findViewById(R.id.gif);
gif.setGifResource(filePath);
gif.play();
int[] location = SystemUtil.locationOnScreen(this);
window = new PopupWindow(gifView, SystemUtil.dipToPx(100), SystemUtil.dipToPx(100));
window.setAnimationStyle(R.style.Dialog);
window.setBackgroundDrawable(getResources().getDrawable(R.drawable.shape_trans_while));
window.setFocusable(true);
window.setOutsideTouchable(true);
window.update();
//location[1] = location[1] - getHeight();
if ((location[0] - SystemUtil.dipToPx(25) - SystemUtil.dipToPx(10)) <= SystemUtil.dipToPx(10)) {
location[0] = SystemUtil.dipToPx(10);
} else if ((location[0] - SystemUtil.dipToPx(25) + SystemUtil.dipToPx(100) + SystemUtil.dipToPx(20)) >= SystemDataUtil.getScreenWidth()) {
location[0] = SystemDataUtil.getScreenWidth() - SystemUtil.dipToPx(10) - SystemUtil.dipToPx(100);
} else {
location[0] = location[0] - SystemUtil.dipToPx(25);
}
window.showAtLocation(this, Gravity.TOP | Gravity.START, location[0], location[1] - SystemUtil.dipToPx(100));
}
/**
* 显示底部菜单栏
*
* @param popupWindow
*/
public void showBottomPopupMenu(final PopupWindow popupWindow, final int background)
{
popupWindow.showAtLocation(rocky_rootView, Gravity.BOTTOM, 0, 0);
getUiHandler().postDelayed(new Runnable()
{
@Override
public void run()
{
setBottomPopupMenuBackground(background);
setBottomPopupMenuVisibility(View.VISIBLE);
}
}, 500);
}
/**
* 显示
*/
public void show() {
dismiss();
initAnim();
layout = (RelativeLayout) layoutInflater.inflate(R.layout.progress_bar_layout, null);
circleView = (View) layout.findViewById(R.id.loading_dialog);
layout_bg = (RelativeLayout) layout.findViewById(R.id.bgLayout);
popupDialog = new PopupWindow(layout, ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT);
View parentView = ((Activity) context).getWindow().findViewById(Window.ID_ANDROID_CONTENT);
popupDialog.showAtLocation(parentView, Gravity.CENTER, 0, 0);
layout_bg.startAnimation(alphaAnim_in);
circleView.startAnimation(rotateAnim);
}
private void showShareLayout() {
mSharePopWindow = new PopupWindow(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
View view = LayoutInflater.from(this).inflate(R.layout.sharelayout, null);
view.findViewById(R.id.btn_share_to_circle).setOnClickListener(this);
view.findViewById(R.id.btn_share_to_friend).setOnClickListener(this);
mSharePopWindow.setContentView(view);
mSharePopWindow.setFocusable(false);
mSharePopWindow.setBackgroundDrawable(new BitmapDrawable());
mSharePopWindow.setOutsideTouchable(true);
mSharePopWindow.showAtLocation(findViewById(android.R.id.content), Gravity.BOTTOM,0,0);
}
@Override
protected View initComponentHostView(@NonNull Context context) {
mContainerView = new WXMaskView(context);
mPopupWindow = new PopupWindow(context);
mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
mPopupWindow.setAttachedInDecor(true);
}
//setClippingEnabled(false) will cause INPUT_ADJUST_PAN invalid.
//mPopupWindow.setClippingEnabled(false);
mPopupWindow.setContentView(mContainerView);
mPopupWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
mPopupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
mPopupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN |
WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
mPopupWindow.setFocusable(true);
mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
fireVisibleChangedEvent(false);
}
});
int y = 0;
int statusBarHeight = 0;
int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
statusBarHeight = context.getResources().getDimensionPixelSize(resourceId);
y = statusBarHeight;
}
mPopupWindow.showAtLocation(((Activity) context).getWindow().getDecorView(),
Gravity.TOP | Gravity.START,
0,
y);
fireVisibleChangedEvent(true);
return mContainerView;
}
private void showKey(final int keyIndex) {
final PopupWindow previewPopup = mPreviewPopup;
final Key[] keys = mKeys;
if (keyIndex < 0 || keyIndex >= mKeys.length) return;
Key key = keys[keyIndex];
if (key.icon != null) {
mPreviewText.setCompoundDrawables(null, null, null,
key.iconPreview != null ? key.iconPreview : key.icon);
mPreviewText.setText(null);
} else {
mPreviewText.setCompoundDrawables(null, null, null, null);
mPreviewText.setText(getPreviewText(key));
if (key.label.length() > 1 && key.codes.length < 2) {
mPreviewText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mKeyTextSize);
mPreviewText.setTypeface(Typeface.DEFAULT_BOLD);
} else {
mPreviewText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mPreviewTextSizeLarge);
mPreviewText.setTypeface(Typeface.DEFAULT);
}
}
mPreviewText.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
int popupWidth = Math.max(mPreviewText.getMeasuredWidth(), key.width
+ mPreviewText.getPaddingLeft() + mPreviewText.getPaddingRight());
final int popupHeight = mPreviewHeight;
LayoutParams lp = mPreviewText.getLayoutParams();
if (lp != null) {
lp.width = popupWidth;
lp.height = popupHeight;
}
if (!mPreviewCentered) {
mPopupPreviewX = key.x - mPreviewText.getPaddingLeft() + mPaddingLeft;
mPopupPreviewY = key.y - popupHeight + mPreviewOffset;
} else {
// TODO: Fix this if centering is brought back
mPopupPreviewX = 160 - mPreviewText.getMeasuredWidth() / 2;
mPopupPreviewY = - mPreviewText.getMeasuredHeight();
}
mHandler.removeMessages(MSG_REMOVE_PREVIEW);
getLocationInWindow(mCoordinates);
mCoordinates[0] += mMiniKeyboardOffsetX; // Offset may be zero
mCoordinates[1] += mMiniKeyboardOffsetY; // Offset may be zero
// Set the preview background state
mPreviewText.getBackground().setState(
key.popupResId != 0 ? LONG_PRESSABLE_STATE_SET : EMPTY_STATE_SET);
mPopupPreviewX += mCoordinates[0];
mPopupPreviewY += mCoordinates[1];
// If the popup cannot be shown above the key, put it on the side
getLocationOnScreen(mCoordinates);
if (mPopupPreviewY + mCoordinates[1] < 0) {
// If the key you're pressing is on the left side of the keyboard, show the popup on
// the right, offset by enough to see at least one key to the left/right.
if (key.x + key.width <= getWidth() / 2) {
mPopupPreviewX += (int) (key.width * 2.5);
} else {
mPopupPreviewX -= (int) (key.width * 2.5);
}
mPopupPreviewY += popupHeight;
}
if (previewPopup.isShowing()) {
previewPopup.update(mPopupPreviewX, mPopupPreviewY,
popupWidth, popupHeight);
} else {
previewPopup.setWidth(popupWidth);
previewPopup.setHeight(popupHeight);
previewPopup.showAtLocation(mPopupParent, Gravity.NO_GRAVITY,
mPopupPreviewX, mPopupPreviewY);
}
mPreviewText.setVisibility(VISIBLE);
}
public void show() {
if (mAnchorView == null) {
throw new IllegalArgumentException("PopupWindow show location view can not be null");
}
if (mItemData == null) {
throw new IllegalArgumentException("please fill ListView Data");
}
mPopView = new ListView(mContext);
mPopView.setBackgroundColor(ContextCompat.getColor(mContext, android.R.color.white));
mPopView.setVerticalScrollBarEnabled(false);
mPopView.setDivider(null);
mPopView.setAdapter(new ArrayAdapter<>(mContext,
android.R.layout.simple_list_item_1, mItemData));
if (mItemClickListener != null) {
mPopView.setOnItemClickListener(mItemClickListener);
}
mPopView.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
if (mPopupWindowWidth == 0) {
mPopupWindowWidth = mDeviceWidth / 3;
}
if (mPopupWindowHeight == 0) {
mPopupWindowHeight = mItemData.size() * mPopView.getMeasuredHeight();
if (mPopupWindowHeight > mDeviceHeight / 2) {
mPopupWindowHeight = mDeviceHeight / 2;
}
}
mPopupWindow = new PopupWindow(mPopView, mPopupWindowWidth, mPopupWindowHeight);
if (mPopAnimStyle != 0) {
mPopupWindow.setAnimationStyle(mPopAnimStyle);
}
mPopupWindow.setOutsideTouchable(true);
mPopupWindow.setFocusable(mModal);
mPopupWindow.setBackgroundDrawable(new BitmapDrawable(mContext.getResources(), (Bitmap) null));
Rect location = locateView(mAnchorView);
if (location != null) {
int x;
//view中心点X坐标
int xMiddle = location.left + mAnchorView.getWidth() / 2;
if (xMiddle > mDeviceWidth / 2) {
//在右边
x = xMiddle - mPopupWindowWidth;
} else {
x = xMiddle;
}
int y;
//view中心点Y坐标
int yMiddle = location.top + mAnchorView.getHeight() / 2;
if (yMiddle > mDeviceHeight / 2) {
//在下方
y = yMiddle - mPopupWindowHeight;
} else {
//在上方
y = yMiddle;
}
mPopupWindow.showAtLocation(mAnchorView, Gravity.NO_GRAVITY, x, y);
}
}