我的上一篇文章:设置DialogFragment全屏显示 可以设置对话框的内容全屏显示,但是存在在某些机型上顶部的View被状态栏遮住的问题。经过测试,发现了一种解决办法,在DialogFragment的onCreateView()中添加一个布局监听器:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { ... 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) { ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams)topMarginView.getLayoutParams(); params.topMargin += BarUtils.getStatusBarHeight(); topMarginView.setLayoutParams(params); rootView.getViewTreeObserver().removeOnGlobalLayoutListener(this); } } }); }
|
这种方法是通过监听对话框内容布局顶层View在屏幕中的位置来解决的,如果顶层View在屏幕中的y位置为0,则表示其已经被状态栏所遮住,然后将被遮住的View向下移动状态栏的高度即可。
这种方式显然不够优雅,如果读者能有更好的方法,欢迎留言。
本文地址,如有更多疑问,请参考我的其它Android相关博客:我的博客地址