How to set screen size to full screen at runtime in android

本小妞迷上赌 提交于 2019-12-11 11:46:27

问题


I want to change the screen layout in my application to full screen when the user click on button but it didn't work, my code is:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    btnFullScreen = (Button) findViewById(R.id.btnFullScreen);
    btnNormalScreen = (Button) findViewById(R.id.btnNormalScreen);

    btnFullScreen.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            setTheme(R.style.AppBaseThemeFullScreen);

        }
    });

    btnNormalScreen.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            setTheme(R.style.AppBaseTheme);

        }
    });



}

And my Full Screen theme is:

<style name="AppBaseThemeFullScreen" parent="android:Theme.Light">
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowNoTitle">true</item>
</style>

And my Normal theme is

<style name="AppBaseTheme" parent="android:Theme.Light">
    <item name="android:windowNoTitle">true</item>
</style>

So if there is any way to do that please help me.


回答1:


void toggleFullScreen(boolean goFullScreen){   
    if(goFullScreen){
        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);
    }

    yourView.requestLayout();
}



回答2:


If you want to change the theme from the code, it seems you have to do it before calling setContentView().

You can also try:

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                            WindowManager.LayoutParams.FLAG_FULLSCREEN);


来源:https://stackoverflow.com/questions/15633313/how-to-set-screen-size-to-full-screen-at-runtime-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!