简体   繁体   English

安卓 如何在按钮正上方显示弹出窗口

[英]Android. How to show popup window directly above button

I need to show popup window directly above button. 我需要在按钮上方直接显示弹出窗口。 My button is placed inside scroll view, and popup always shown below button. 我的按钮位于滚动视图中,弹出窗口始终显示在按钮下方。 Here is my code: 这是我的代码:

  private void showPopup(View view, String text) {
    if (infoPopup == null) {
        LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
        View popupView = layoutInflater.inflate(R.layout.popup_credit_request_passport, null);
        TextView tvPopupText = popupView.findViewById(R.id.tv_popup_text);
        tvPopupText.setMovementMethod(new ScrollingMovementMethod());
        tvPopupText.setText(text);
        FrameLayout flBackground = popupView.findViewById(R.id.fl_background);
        flBackground.setBackground(new BubbleDrawable(getContext(), R.color.azure, 16, 16, 8));

        infoPopup = new PopupWindow(popupView,
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        infoPopup.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        infoPopup.setOutsideTouchable(true);

        infoPopup.showAtLocation(view, Gravity.TOP, (int) view.getX(), (int) view.getY());

    } else {
        dismissInfoPopup();
    }
}

But it is not work. 但这是行不通的。 Please hep me. 请帮我 在此处输入图片说明

将“ 重力”更改为“顶部”,然后根据需要重置坐标。

infoPopup.showAtLocation(view, Gravity.TOP, 0, (int) view.getY());

当您必须显示弹出窗口时,找出按钮在屏幕上的位置,然后使用Gravity.TOP和按钮在屏幕上的x,y coordinates ,如Prachi Said一样使用Gravity.TOP showAtLocation()方法...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM