我的上一篇文章:设置DialogFragment全屏显示 可以设置对话框的内容全屏显示,但是存在在某些机型上顶部的View被状态栏遮住的问题。经过测试,发现了一种解决办法,在DialogFragment的onCreateView()中添加一个布局监听器:

@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { ... //此处rootView是对话框的顶层View rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { int[] location = new int[2]; rootView.getLocationOnScreen(location); int y = location[1]; if (y == 0) { //此处的topMarginView是被状态栏覆盖的View ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams)topMarginView.getLayoutParams(); params.topMargin += BarUtils.getStatusBarHeight(); topMarginView.setLayoutParams(params); rootView.getViewTreeObserver().removeOnGlobalLayoutListener(this); } } });}

这种方法是通过监听对话框内容布局顶层View在屏幕中的位置来解决的,如果顶层View在屏幕中的y位置为0,则表示其已经被状态栏所遮住,然后将被遮住的View向下移动状态栏的高度即可。

这种方式显然不够优雅,如果读者能有更好的方法,欢迎留言。

本文地址,如有更多疑问,请参考我的其它Android相关博客:我的博客地址