Android: how to hide title bar on button click

2019-08-16 23:51发布

问题:

App crashing when using requestWindowFeature(Window.FEATURE_NO_TITLE);

When using the button to make it full screen which when tries to hide the Title bar the app crashes

static int vari = 0;
public void fsc(){
    ib = (ImageButton) findViewById(R.id.fulls);
    ib.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(getApplicationContext(), "Full-Screen", Toast.LENGTH_LONG).show();
            if(vari == 0)
            {
requestWindowFeature(Window.FEATURE_NO_TITLE);
   getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);

getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
                    vari = 1;

            }
        });

I wanted to make it full screen (hiding both status and title bar) on the button press

Please note: this is also to be called to fragments

回答1:

Check if you have the fullscreen theme set in the manifest?

//if not add this in your manifest

android:theme="@android:style/Theme.NoTitleBar.Fullscreen"

// Hide status bar

getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

// Show status bar

getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);