android - 弹出 showAtLocation 不起作用

标签 android android-popupwindow

我在单击 edit-text 时使用弹出窗口显示一些 TextView 。但弹出窗口没有显示在特定位置,它总是显示在左上角,这是我的代码,哪里出了问题

    private void showPopup(Context context,final LinearLayout Parent) {


    LayoutInflater layoutInflater = (LayoutInflater)context
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    layout = layoutInflater.inflate(R.layout.popup,MainActivity.parent,true);
    // Creating the PopupWindow
    final PopupWindow popupWindow = new PopupWindow(
               layout,700,700);


    popupWindow.setContentView(layout);
    popupWindow.setHeight(500);


    new Runnable(){
        @Override
        public void run() {

            popupWindow.showAtLocation(layout, Gravity.CENTER,300,150);
        }

    };


    }

最佳答案

尝试以下代码:

public static Rect locateView(View v)
{
    int[] loc_int = new int[2];
    if (v == null) return null;
    try
    {
        v.getLocationOnScreen(loc_int);
    } catch (NullPointerException e)
    {
        //Happens when the view doesn't exist on screen anymore.
        return null;
    }
    Rect location = new Rect();
    location.left = loc_int[0];
    location.top = loc_int[1];
    location.right = location.left + v.getWidth();
    location.bottom = location.top + v.getHeight();
    return location;
}

然后使用

popup.showAtLocation(parent, Gravity.TOP|Gravity.LEFT, location.left, location.bottom);

希望对你有帮助...

关于android - 弹出 showAtLocation 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23992129/

相关文章:

android - MIUI中如何开启Draw Overlay权限弹窗?

android - 在 Android 中更改背景 popupMenu

android - 在 Android 手机上使用主页按钮

java - Android VideoView 中的纵向视频不会占据整个屏幕宽度

android - 用 Admob 广告替换 Google Adsense 广告

android - 获取弹出窗口内 View 的绝对坐标

PopupWindow onitemclick 中的 Android Listview 在某些设备上不起作用

android - 弹出窗口未在 android L 中正确显示

java - 需要 OOP 和继承建议

android - getXml(id) - 未找到资源异常