【问题标题】:android - requestWindowFeature(Window.FEATURE_NO_TITLE) exceptionandroid - requestWindowFeature(Window.FEATURE_NO_TITLE) 异常
【发布时间】:2014-01-25 18:17:36
【问题描述】:

当用户点击开始按钮时,我正在设置一个特定的活动全屏。

在这种情况下,showStopButton() 被调用。

运行良好。但是如果我插入

 requestWindowFeature(Window.FEATURE_NO_TITLE); 

然后它崩溃了,说明应该在添加内容之前调用它。

我应该如何处理它以将NO_TITLE 设置为FULL_SCREEN

    private void showStopButton(){

    // requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
    getWindow().findViewById(android.R.id.content).requestLayout();

    // handle element visibility
((Button)findViewById(R.id.stopButton)).setEnabled(false);
    ((Button)findViewById(R.id.startButton)).setVisibility(View.GONE);
    ((Button)findViewById(R.id.stopButton)).setVisibility(View.VISIBLE);
    ((SeekBar)findViewById(R.id.seekBar1)).setVisibility(View.VISIBLE);
    ((Button)findViewById(R.id.resetButton)).setVisibility(View.GONE);
    ((Button)findViewById(R.id.saveButton)).setVisibility(View.GONE);
}

当重新显示开始按钮时,我有相反的过程,它运行正常 在这种情况下,我删除了全屏模式

     private void showStartButton(){

        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().findViewById(android.R.id.content).requestLayout();
        ....
    }

【问题讨论】:

  • 你必须在“setContentView()”之前调用“requestWindowFeature”。

标签: android android-fullscreen


【解决方案1】:

很简单...我只需要隐藏ActionBar...然后在返回标准屏幕时显示...

   private void showStopButton(){
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
        ActionBar actionBar = getActionBar();
        actionBar.hide();
        getWindow().findViewById(android.R.id.content).requestLayout();

【讨论】:

    【解决方案2】:

    使用 -public class MainActivity extends Activity- 而不是 -public class MainActivity extends ActionBarActivity-

    【讨论】:

      【解决方案3】:

      所以:

      @Override
      protected void onCreate(
          final Bundle savedInstanceState)
      {
          super.onCreate(savedInstanceState);
      
          // Make this activity, full screen
          getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
              WindowManager.LayoutParams.FLAG_FULLSCREEN);
      
          // Hide the Title bar of this activity screen
          getWindow().requestFeature(Window.FEATURE_NO_TITLE);
      
          setContentView(R.layout.main);
      
          // MORE INIT STUFF HERE...
          //img = (ImageView) findViewById(R.id.imgRandom);
          //btnRandom = (Button) findViewById(R.id.btnRandom);
      }
      

      【讨论】:

      • 是的,这是有效的,在 onCreate() 方法中......但我试图在 showStop() 方法中隐藏它......我睡了一会儿后找到了解决方案......
      【解决方案4】:

      试试这个……

      public class MainActivity extends Activity {
      
          Context context;
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
      
              context = this;
      
              requestWindowFeature(Window.FEATURE_NO_TITLE);
              getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                      WindowManager.LayoutParams.FLAG_FULLSCREEN);
      
              setContentView(R.layout.activity_main);
          }
      }
      

      【讨论】:

        【解决方案5】:

        添加这个。

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        

        之前

         super.onCreate(savedInstanceState);
         setContentView(R.layout.your activity);
        

        【讨论】:

          【解决方案6】:

          可能是这样的:

          public class MainActivity extends AppCompatActivity {
             ...
              private final Handler handler = new Handler();
          
              @Override
              protected void onCreate(Bundle savedInstanceState) {
                  super.onCreate(savedInstanceState);
          
                  requestWindowFeature(Window.FEATURE_NO_TITLE);
                  getSupportActionBar().hide(); 
                  getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
                  getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
                  setContentView(R.layout.activity_main);
          

          我在警车灯和警报器的例子中使用了上面的代码。当应用程序启动时,灯光动画自动启动,警报器在后台响起。

          来源:Android police lights & siren

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2022-07-19
            • 1970-01-01
            • 2013-01-16
            • 1970-01-01
            • 2017-06-20
            相关资源
            最近更新 更多