点击显示和隐藏NavigationBar和ActionBar | Android

3

我希望你能够在屏幕点击时隐藏和显示操作栏和导航栏(无论在屏幕的哪个位置),并在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活动中添加了全屏标志。 谢谢。

编辑2


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);
}

使用onuserinteraction()方法 - sunil
在你的清单文件中,android:theme="@style/Theme.Sherlock.NoActionBar" 这个主题被使用了吗? - Sagar Pilkhwal
1个回答

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);

这仅在触摸后才起作用。首先,导航栏和状态栏会显示。第二次触摸后,操作栏会显示,一秒钟后操作栏会隐藏,而导航栏和状态栏则保持在屏幕上。第三次触摸后,导航栏和状态栏都会隐藏。 - Alon
删除fullScreenLandSpace()函数,使用我展示的onUserInteraction()函数,并通知我。 - sunil
状态栏仍然显示在屏幕上,我完全不想要它。 - Alon
谢谢,我找到了问题所在。我使用了vitamio,导致冲突。我将更改vitamio中的代码,然后它就可以工作了!谢谢。 - Alon

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接