5

我试图在用户触摸屏幕的任何地方显示弹出窗口。我能够在所需位置显示弹出窗口。但是问题是当弹出窗口处于纵向模式时,如果我在横向模式下更改弹出窗口显示在同一位置的方向,因为弹出窗口与横向模式下的视图重叠,并且当我们将方向横向更改为纵向时会出现同样的问题. 我的要求低于 1. 更改方向时不想关闭弹出窗口。2. 动态改变所有弹出窗口的位置,每当方向改变时视图不重叠(弹出窗口不与图像重叠)。例如,当我将方向纵向更改为横向弹出窗口位置时,位置将向上移动。

private void showPopup(final Activity context, Point p) {
    int popupWidth = 200;
    int popupHeight = 150;
    boolean showEditText = true;
    // Inflate the popup_layout.xml
    LinearLayout viewGroup = (LinearLayout) context
            .findViewById(R.id.popup);
    LayoutInflater layoutInflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View layout = layoutInflater.inflate(R.layout.popup_layout, viewGroup);

    // Creating the PopupWindow
    final PopupWindow popup = new PopupWindow(context);
    popup.setContentView(layout);
    popup.setWidth(popupWidth);
    popup.setHeight(popupHeight);
    popup.setFocusable(false);
    popup.setOutsideTouchable(false);

    // Some offset to align the popup a bit to the right, and a bit down,
    // relative to button's position.
    int OFFSET_X = 5;
    int OFFSET_Y = 5;

    // Clear the default translucent background
    popup.setBackgroundDrawable(new BitmapDrawable());

    // Displaying the popup at the specified location, + offsets.
    popup.showAtLocation(layout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y
            + OFFSET_Y);

    final Button addname = (Button) layout.findViewById(R.id.addName);

}
4

1 回答 1

0

我知道这不是一个好的解决方案,但我们可以这样做

制作popup为类级对象

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            if (popupWindow != null && popupWindow.isShowing()) {
                popupWindow.dismiss();
                showPopup(...);
            }
        }
    },500);
}
于 2019-09-23T04:55:53.430 回答