Fullscreen switching

2019-09-11 04:23发布

问题:

I want to control a full screen of the phone with toggle button. I did it, but it works only once. How i can fix it? There is code:

final ToggleButton toggle_button_for_full_screen = (ToggleButton) findViewById(R.id.toggleButton1);
toggle_button_for_full_screen.setOnCheckedChangeListener(new OnCheckedChangeListener() {
boolean variable_for_saving_toggle_button_status;

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        // TODO Auto-generated method stub
        if (isChecked)
            {
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
            }
        else
            {
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
            }
    }
});

回答1:

Try below code for to do this:

if (isChecked)
            {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
                getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
            }
        else
            {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
                getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
            }

It work fine with me.