显示和隐藏NavigationBar和ActionBar OnClick | Android

时间:2014-09-08 11:34:31

标签: java android android-actionbar

我想隐藏并在屏幕上点击操作栏和导航栏(我不在乎屏幕上的位置)并在2秒后隐藏它们。

我读到我可以使用它                   Handler h = new Handler();

               h.postDelayed(new Runnable() {

                @Override
                public void run() {
                // DO DELAYED STUFF
                   getActionBar().hide();
                 }
                 }, delaytime);

但我的问题是点击什么都没发生。

我想要的另一个问题屏幕在没有活动时不会变黑。

修改


我试过这个

public void onUserInteraction() {
    // TODO Auto-generated method stub

    super.onUserInteraction();
    getActionBar().show();
    new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {

            getActionBar().hide();

        }
    }, 1000);
}

但是当我触摸屏幕时会显示状态栏。并且仅在秒触摸操作栏显示。操作栏在1秒后进行,但状态栏和导航栏保持不变。 我不想要状态栏,我也想要在操作栏隐藏时隐藏导航栏。

**我在Oncreat artivity上添加了FULL SCREEN FLAG。 谢谢

EDIT2


public void fullScreenLandSpace(){
    //This function configure FullScreen and LandSpace Only!
    getActionBar().setDisplayShowTitleEnabled(false);
    getActionBar().setDisplayShowHomeEnabled(false);
    View decorView = getWindow().getDecorView();
    int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN|View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
    decorView.setSystemUiVisibility(uiOptions);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    getActionBar().hide();

}
@Override
public void onUserInteraction() {
    // TODO Auto-generated method stub

    super.onUserInteraction();
    getActionBar().show();
    getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
    new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {

            getActionBar().hide();
             getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION|View.SYSTEM_UI_FLAG_FULLSCREEN);

        }
    }, 1000);
}

1 个答案:

答案 0 :(得分:4)

   @Override
    public void onUserInteraction() {
        // TODO Auto-generated method stub
        super.onUserInteraction();
        new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                View decorView = getWindow().getDecorView();
                // Hide the status bar.
                int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN|View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                        ;
                decorView.setSystemUiVisibility(uiOptions);
                // Remember that you should never show the action bar if the
                // status bar is hidden, so hide that too if necessary.
                ActionBar actionBar = getActionBar();
                actionBar.hide();
            }
        }, 1000);
    }

// --------------------------------------------- ------ //添加以下代码

View decorView = getWindow().getDecorView();
// Hide the status bar.
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
// Remember that you should never show the action bar if the
// status bar is hidden, so hide that too if necessary.
ActionBar actionBar = getActionBar();
actionBar.hide();

// -------------------------------

View decorView = getWindow().getDecorView();
// Hide both the navigation bar and the status bar.
// SYSTEM_UI_FLAG_FULLSCREEN is only available on Android 4.1 and higher, but as
// a general rule, you should design your app to hide the status bar whenever you
// hide the navigation bar.
int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
              | View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);