android全屏的实现方案1

/**
     * @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.
        }


    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值