/**
* @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.
}
}
android全屏的实现方案1
最新推荐文章于 2020-09-21 18:59:32 发布