【问题标题】:How to restore the popup window's position when changes the orientation in android?在android中更改方向时如何恢复弹出窗口的位置?
【发布时间】:2019-09-23 12:55:53
【问题描述】:

我试图在用户触摸屏幕的任何地方显示弹出窗口。我能够在所需位置显示弹出窗口。但是问题是当弹出窗口处于纵向模式时,如果我在横向模式下更改弹出窗口显示在同一位置的方向,因为弹出窗口与横向模式下的视图重叠,并且当我们将方向横向更改为纵向时会出现同样的问题.我的要求如下 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);

}

【问题讨论】:

    标签: android


    【解决方案1】:

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

    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);
    }
    

    【讨论】:

      猜你喜欢
      • 2012-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-23
      相关资源
      最近更新 更多