我有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。
我可以看到所有旋转后的视图(有一个变暗的屏幕,但它甚至在第一次旋转之前就在那里)
之前和之后看起来一样: