Android 隐藏虚拟按键

领导要求:

  • 横屏播放视频时,不展示虚拟按键;
  • 恢复到竖屏时,展示虚拟按键;
/** * NavigationBar 显示与隐藏 */public class NavigationBarHelper {    private Fragment fragment;    private boolean mSystemUiVisibilityUpdated;    // android 4.4 api19 及以上,记录竖屏时的SystemUiVisibility    private int mSystemUiVisibilityPortrait;    public NavigationBarHelper(Fragment fragment) {        this.fragment = fragment;    }    private View getDecorView() {        if (fragment != null                && fragment.getActivity() != null) {            return fragment.getActivity().getWindow().getDecorView();        }        return null;    }    public void beforeOrientationChange(boolean targetLandscape) {        if (targetLandscape) {            hideBottomMenu();        } else {            showBottomMenu();        }    }    /**     * 竖屏 显示虚拟按键     */    private void showBottomMenu() {        if (fragment != null && fragment.getActivity() != null) {            View decorView = getDecorView();            if (decorView != null) {                if (Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB                        && Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {                    decorView.setSystemUiVisibility(View.VISIBLE);                } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && mSystemUiVisibilityUpdated) {                    // 恢复 竖屏时的SystemUiVisibility                    decorView.setSystemUiVisibility(mSystemUiVisibilityPortrait);                    mSystemUiVisibilityUpdated = false;                }            }        }    }    /**     * 横屏 隐藏虚拟按键     */    private void hideBottomMenu() {        if (fragment != null && fragment.getActivity() != null) {            View decorView = getDecorView();            if (decorView != null) {                if (Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB                        && Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {                    decorView.setSystemUiVisibility(View.GONE);                } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {                    if (mSystemUiVisibilityUpdated) {                        return;                    }                    // 记录竖屏时的SystemUiVisibility                    mSystemUiVisibilityPortrait = decorView.getSystemUiVisibility();                    // 隐藏虚拟按键                    int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION                            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY                            | View.SYSTEM_UI_FLAG_FULLSCREEN;                    decorView.setSystemUiVisibility(uiOptions);                    mSystemUiVisibilityUpdated = true;                }            }        }    }    /**     * 退出到后台 返回时     */    public void onVideoResume() {        if (fragment != null && fragment.getActivity() != null) {            // 当前是横屏 播放视频            if (fragment.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {                View decorView = getDecorView();                if (decorView != null) {                    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB                            && Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {                        decorView.setSystemUiVisibility(View.GONE);                    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {                        // 隐藏虚拟按键                        int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION                                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY                                | View.SYSTEM_UI_FLAG_FULLSCREEN;                        decorView.setSystemUiVisibility(uiOptions);                    }                }            }        }    }}

更多相关文章

  1. 如何在后台运行Linux命令?
  2. Android关于全屏设置和隐藏状态栏、沉浸式状态栏的总结
  3. android之VideoView和视频播放View的扩展
  4. [转]android 获取视频帧
  5. Android(安卓)拿出后台打印数据
  6. android 判断APP是否打开的方法
  7. android常用的几个获取包或activity以及判断前后台等
  8. 腾讯云-视频直播(android集成)
  9. android 获取视频和图片的缩略图

随机推荐

  1. 干货!python与MySQL数据库的交互实战
  2. 用python数据分析了北京积分落户名单,发现
  3. Python22个构造函数法-助力数据挖掘与分
  4. 怎样开发可重用组件并发布到NPM [每日前
  5. Node.js多线程完全指南[每日前端夜话0x43
  6. 7 个Web开发工程师面试题和回答策略 [每
  7. 正则表达式在 ES2018 中的新写法 [每日前
  8. 如何用自己喜欢的 CSS 风格重置网站的样
  9. 如何用原生 DOM API 生成表格 [每日前端
  10. 2019最烂密码榜单出炉,教你设置神级密码!