隐藏状态栏和标题栏特定时间

时间:2018-06-12 06:52:02

标签: android android-fullscreen

我希望在特定时间内以相同的布局隐藏状态和标题栏。喜欢2或3秒。为此,我尝试了 Timer ,据我所知,必须先调用功能请求 requestFeature() SetContentViewSuper.OnCreate。所以,我写了这段代码:

@Override
protected void onCreate(Bundle savedInstanceState) {

    Timer timer = new Timer();
    timer.scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    // Hiding Title bar of this activity screen //
                    getWindow().requestFeature(Window.FEATURE_NO_TITLE);

                    // Making this activity, full screen //
                    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

                }
            });
        }
    }, 1000, 5000);


    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);

但它伴随着以下错误:

android.util.AndroidRuntimeException: requestFeature() must be called before adding content

我认为计时器是造成错误的原因。因为,当我在没有Timer的情况下调用 requestFeature()时,该应用运行时不会显示任何错误或崩溃。

  

有没有办法在同一版面中隐藏特定时间的状态和标题栏? (不是意图)

2 个答案:

答案 0 :(得分:2)

 Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        // Hiding Title bar of this activity screen //
                         getSupportActionBar().hide();
                        // Making this activity, full screen //
                        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

                    }
                });
            }
        }, 1000, 5000);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

getWindow().requestFeature(Window.FEATURE_NO_TITLE);替换为getSupportActionBar().hide();

答案 1 :(得分:0)

使用以下代码

全屏

 // Full Screen call using DecorView
 private void setFullScreen() {
           getWindow().addFlags(LayoutParams.FLAG_FULLSCREEN);
      }

从全屏删除

private void removeFromFullScreen() {
    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
  }

现在在OnCreate()

@Override
protected void onCreate(Bundle savedInstanceState) {

   final android.os.CountDownTimer Count = new android.os.CountDownTimer(5000, 1000) {
 @Override
  public void onTick(long millisUntilFinished) {
   if (millisUntilFinished/1000 == 2) {
      setFFullScreen();
    }
   Log.e("TAG",millisUntilFinished/1000+"");
  }
  @Override
  public void onFinish() {
    removeFromFullScreen();
  }
};
Count.start();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);