如何在android中运行时将屏幕大小设置为全屏

时间:2013-03-26 09:21:12

标签: java android android-layout android-theme

我想在用户点击按钮时将我的应用程序中的屏幕布局更改为全屏,但它不起作用,我的代码是:

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>

所以,如果有任何方法可以帮助我。

2 个答案:

答案 0 :(得分:11)

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

答案 1 :(得分:0)

如果您想从代码中更改主题,似乎您必须在调用setContentView()之前执行此操作。

您也可以尝试:

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