android - requestWindowFeature(Window.FEATURE_NO_TITLE)异常

时间:2014-01-25 18:17:37

标签: android android-fullscreen

当使用点击START按钮时,我正在全屏设置特定活动。

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

运行正常。但如果我插入

 requestWindowFeature(Window.FEATURE_NO_TITLE); 

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

如何设置NO_TITLE w 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);
}

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

     private void showStartButton(){

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

6 个答案:

答案 0 :(得分:3)

它很简单......我只需要隐藏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();

答案 1 :(得分:3)

使用-public类MainActivity扩展Activity-而不是-public类MainActivity扩展ActionBarActivity -

答案 2 :(得分:2)

所以:

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

答案 3 :(得分:0)

试试这个......

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

答案 4 :(得分:0)

添加它。

requestWindowFeature(Window.FEATURE_NO_TITLE);

之前

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

答案 5 :(得分:0)

可能是这样的:

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

enter image description here