更改PopupWindow的重力 [英] Change gravity of PopupWindow

查看:86
本文介绍了更改PopupWindow的重力的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个必须显示弹出窗口的按钮.并且弹出窗口必须与按钮的右侧对齐.这就是我的方法.

I have a button that must show a popup window. And the popup window must be aligned with the right side of the button. That's how I do it.

button.setOnClickListener( new View.OnClickListener() {
    @Override
    public void onClick( final View view ) {
        if(popup == null) {
            final View view = getLayoutInflater().inflate(R.layout.popup, null);
            popup = new PopupWindow(view);
        }

        if(popup.isShowing()) {
            popup.dismiss();
        } else {
            popup.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
            popup.setFocusable(true);
            popup.setWindowLayoutMode(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
            popup.showAsDropDown(button, 0, 0);
        }
    }
} );

此代码可以正常工作,但弹出窗口与按钮的左侧对齐.有没有简单的方法可以更改PopupWindow的重力?

This code works fine but the popup window is aligned with the left side of the button. Is there an easy way to change gravity of PopupWindow?

推荐答案

我找到的最简单的方法是指定x-offset参数:

The easiest way I've found is to specify the x-offset parameter:

final View view = popup.getContentView();
view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
popup.showAsDropDown(button, button.getWidth() - view.getMeasuredWidth(), 0);

这篇关于更改PopupWindow的重力的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆