0

我有AppCompatDialogFragment这个代码:

    View anchorView = getActivity().findViewById("R.id.myView")

 if (ViewCompat.isLaidOut(anchorView)) {
      anchorView.getLocationInWindow(location);
      Log.w(
                      "*****",
          "====alignWithAnchorView:: anchor width="
              + anchorView.getWidth()
              + " anchor x="
              + location[0]
              + " anchor y="
              + location[1]);
      alignWithAnchorView(anchorView);

    } else {
      anchorView
          .getViewTreeObserver()
          .addOnGlobalLayoutListener(
              new OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                  anchorView.getLocationInWindow(location);
                  Log.w(
                      "*****",
                      "====onGlobalLayout:: anchor width="
                          + anchorView.getWidth()
                          + " anchor x="
                          + location[0]
                          + " anchor y="
                          + location[1]);
                  alignWithAnchorView(anchorView);
                  OneGoogleViewCompat.removeOnGlobalLayoutListener(anchorView, this);
                }
              });
    }

我第一次调用它时,我得到了view定位和膨胀,因为它已经布置好了。

但是当我更改为横向时,我得到了anchorView定位(0,0)width ==0

什么会导致这种配置更改?因为视图是由 id 找到的,所以我等待onGlobalLayout

我再旋转一次,大小仍然为 0。

我可以看到所有旋转后的视图(有一个变暗的屏幕,但它甚至在第一次旋转之前就在那里)

之前和之后看起来一样:

在此处输入图像描述

4

0 回答 0