简书链接:android全屏的实现方案1 文章字数:182,阅读全文大约需要1分钟
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 /** * @param activity 用来查找根视图 * @param viewGroup 当退出全屏的时候被插入的父节点 * @param anchor 全屏控件 * @param isPortrait 是否是竖屏 */ public static void setOrRemoveAnchorViewToRootView(Activity activity, ViewGroup viewGroup, View anchor, boolean isPortrait) { FrameLayout contentRootView = (FrameLayout) activity.findViewById(android.R.id.content); if (contentRootView.getChildAt(0) instanceof FrameLayout) { // contentRootView = (FrameLayout) contentRootView.getChildAt(0);//这个是真activity布局。 } boolean requestCloseFull = anchor.getParent() == contentRootView; // boolean requestFull = anchor.getParent() == viewGroup;//现在布局进行调整了 是一个线性布局,那么父容器变化了。 ((ViewGroup) anchor.getParent()).removeView(anchor); boolean hasParent = anchor.getParent() != null; FrameLayout.LayoutParams params = null; if (isPortrait) { if (!requestCloseFull) { params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, AppContext.getDisplayMetrics().heightPixels);//全屏 写死 isPorrait 不旋转的情况下不写死那么会出问题。??我已经忘记为什么这么写了岁月是一把杀猪刀 } else { params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); } } else {//要居中还要白屏 params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, requestCloseFull ? ViewGroup.LayoutParams.WRAP_CONTENT : ViewGroup.LayoutParams.MATCH_PARENT); } params.gravity = Gravity.CENTER; if (anchor.getTag(R.id.video_top) != null) {//取消全屏 anchor.setTag(R.id.video_top, null); viewGroup.addView(anchor); } else {//全屏 anchor.setTag(R.id.video_top, "qssq"); contentRootView.addView(anchor, contentRootView.getChildCount(), params);//ava.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. } }