有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java无法根据其大小定位安卓 popupwindow(安卓 API8)

我想根据弹出窗口的大小,将弹出窗口定位为另一个视图+偏移上方的工具提示

我尝试了几种方法都没有成功:

1)尝试1:使用showAtPosition()两次

// The method that displays the popup.
  public void showPopup(View viewToPointAt) {

    this.mViewToPointAt = viewToPointAt;

    mPopUpView = mActivity.getLayoutInflater().inflate(R.layout.tooltip_share, null);
    mPopup =
        new PopupWindow(mPopUpView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);

    mPopup.showAtLocation(mPopUpView, Gravity.NO_GRAVITY, 0, 0); // Displaying popup


    ViewTreeObserver vto = mPopUpView.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
      @Override
      public void onGlobalLayout() {
        mPopUpView.getViewTreeObserver().removeGlobalOnLayoutListener(this);


    mArrowPoint = new Point();
    mPopupRect = locatePopup(mPopUpView);
    mArrowPoint.x = mPopupRect.right - DisplayUtils.dpiToPixels(10);
    mArrowPoint.y = mPopupRect.bottom;


    mRectToPointAt = locateView(mViewToPointAt);


        mPopUpView.bringToFront();
        mPopup.setFocusable(false);
        mPopup.setOutsideTouchable(true);




    mPopup.showAtLocation(mPopUpView, Gravity.NO_GRAVITY,
 mRectToPointAt.right - mArrowPoint.x,
            mRectTo

PointAt.top - mArrowPoint.y); // Displaying popup

      }
    });

  }

结果:弹出窗口仍处于(0,0)

2)尝试2:使用update(..)

// The method that displays the popup.
  public void showPopup(View viewToPointAt) {

    this.mViewToPointAt = viewToPointAt;

    mPopUpView = mActivity.getLayoutInflater().inflate(R.layout.tooltip_share, null);
    mPopup =
        new PopupWindow(mPopUpView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);

    mPopup.showAtLocation(mPopUpView, Gravity.NO_GRAVITY, 0, 0); // Displaying popup


    ViewTreeObserver vto = mPopUpView.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
      @Override
      public void onGlobalLayout() {
        mPopUpView.getViewTreeObserver().removeGlobalOnLayoutListener(this);


    mArrowPoint = new Point();
    mPopupRect = locatePopup(mPopUpView);
    mArrowPoint.x = mPopupRect.right - DisplayUtils.dpiToPixels(10);
    mArrowPoint.y = mPopupRect.bottom;


    mRectToPointAt = locateView(mViewToPointAt);


        mPopUpView.bringToFront();
        mPopup.setFocusable(false);
        mPopup.setOutsideTouchable(true);


        mPopup.update(mViewToPointAt, 0, 0, -1, -1);

      }
    });

  }

结果:弹出窗口与屏幕底部对齐。不管我给它多大的偏移量

我的工具提示\u共享。xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    安卓:id="@+id/tooltip_layout"
    安卓:layout_width="290dp"
    安卓:layout_height="wrap_content"
    安卓:background="@drawable/tip_tool_top_right"
    安卓:gravity="center_vertical"
    安卓:orientation="horizontal"
    安卓:paddingBottom="15dp"
    安卓:paddingLeft="0dp"
    安卓:paddingRight="25dp"
    安卓:paddingTop="5dp" >

    <LinearLayout
        安卓:layout_width="wrap_content"
        安卓:layout_height="match_parent"
        安卓:gravity="center"
        安卓:orientation="horizontal" >

        <ImageView
            安卓:layout_width="wrap_content"
            安卓:layout_height="wrap_content"
            安卓:layout_marginLeft="10dp"
            安卓:layout_marginRight="10dp"
            安卓:src="@drawable/tip_tool_icon_plane" />
    </LinearLayout>


</LinearLayout>

我做错了什么


共 (2) 个答案

  1. # 2 楼答案

    您必须创建弹出窗口。xml符合您的弹出式设计。。。之后,您必须随时调用popup

    在创建时编写代码:

    LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
        popupView = layoutInflater.inflate(R.layout.popup, null);
        popupWindow = new PopupWindow(popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setFocusable(true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
    

    点击按钮:

    popupWindow.showAsDropDown(btn_share, 50, -30);