[Android]在沉浸式粘性模式下以编程方式显示状态栏和导航栏

时间:2015-12-11 07:47:20

标签: java android

我使用以下代码在Android中进入沉浸式粘贴模式。

mDecorView.setSystemUiVisibility(
        View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
        | View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
        | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);

我想要的是:我希望状态栏和导航栏在我触摸活动的中心部分时显示,而不是从边缘滑动。这可能吗?

1 个答案:

答案 0 :(得分:1)

如果您不想编写任何其他代码(只需调用某些方法),请从此处使用SystemUIHelper:https://gist.github.com/chrisbanes/73de18faffca571f7292

然后是这样的:

...

    SystemUiHelper uiHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.[Your content view]);
        uiHelper = new SystemUiHelper(this, [Your flags/modes]);
      
        // Then set onTouchListener on youк root view
        // and when someone touch it you will receive an event about it 
        // and will be able to manage it and hide/show app's UI again
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_UP:
                if (uiHelper.isShowing()){
                    uiHelper.hide();
                }else{
                    uiHelper.show();
                }
                break;
        }
      
        return super.onTouchEvent(event);
    }