如何在Android的运行时的屏幕尺寸设置为全屏(How to set screen size to

2019-10-17 21:38发布

我想改变我的应用程序以全屏幕画面布局,当用户点击按钮,但它没有工作, 我的代码是:

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

        }
    });



}

而我的全屏的主题是:

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

而我的正常主题

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

所以,如果有任何的方式来做到这一点,请帮助我。

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


Answer 2:

如果你想从代码更改主题,看来你有打电话之前做它setContentView()

您也可以尝试:

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


文章来源: How to set screen size to full screen at runtime in android