26

如何在 Android 中为活动设置全屏模式?我正在使用以下代码设置全屏,但它会产生错误:

例外:

android.util.AndroidRuntimeException: 
    requestFeature() must be called before adding content.         

代码:

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

4 回答 4

61

请检查代码

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

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

    setContentView(R.layout.main);
}

并注意它是在设置内容视图之前设置的

于 2012-09-12T13:02:43.167 回答
13

在 AndroidManifest 中试试这个:

<activity android:name=".ActivityName"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
</activity>
于 2012-09-12T13:02:48.240 回答
4
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                        WindowManager.LayoutParams.FLAG_FULLSCREEN);
于 2012-09-12T13:09:07.803 回答
2

requestWindowFeature首先放在你的代码中......像这样......

requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
于 2012-09-12T13:11:28.773 回答