2

全屏在我的应用程序中,我编写了以下代码:

  public void onCreate(Bundle savedInstanceState) {
         requestWindowFeature(Window.FEATURE_NO_TITLE);
         setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
           getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN); 
               super.onCreate(savedInstanceState); 
        setContentView(R.layout.main);
                       .
                       .
                       .

并在我的应用程序中保持屏幕打开,我在布局中编写此代码:

android:keepScreenOn="true" 

尽管并非总是如此,但有时会像这样错误我:

java.lang.IllegalArgumentException: View not attached to window manager
at android.view.WindowManagerImpl.findViewLocked(WindowManagerImpl.java:381)
at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:226)
at android.view.Window$LocalWindowManager.removeView(Window.java:432)
at android.app.Dialog.dismissDialog(Dialog.java:278)
at android.app.Dialog.access$000(Dialog.java:71)
at android.app.Dialog$1.run(Dialog.java:111)
at android.app.Dialog.dismiss(Dialog.java:268)
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3691)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
at dalvik.system.NativeStart.main(Native Method)

我能做些什么?我写的是真实的代码?谢谢...

4

4 回答 4

7
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);

必须先调用 super.onCreate....

您可以以编程方式执行此操作:

public class ActivityName extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // remove title
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.main);
    }
}

或者您可以通过您的 AndroidManifest.xml 文件执行此操作:

<activity android:name=".ActivityName"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
</activity>
于 2012-04-22T06:59:46.543 回答
2

在 AndroidMenifest.xml 文件中添加活动

 android:name=".ActivityName"
        android:configChanges="orientation|keyboardHidden"
        android:keepScreenOn="true"
        android:label="@string/app_name"
        android:theme="@style/customTheme" >

如果在 setContentView 之前存在,则删除以下内容

boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
于 2012-04-22T07:02:47.073 回答
1

要启用全屏:

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);

要启用屏幕:

getWindow().addFlags( WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | 
                        WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
于 2020-07-01T05:21:05.343 回答
0

WindowManager.LayoutParams.FLAG_FULLSCREEN API 折旧 >= 30 (R) 建议使用此方法检查。

@Suppress("DEPRECATION")
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
   window.insetsController?.hide(WindowInsets.Type.statusBars())
} else {
   window.setFlags(
      WindowManager.LayoutParams.FLAG_FULLSCREEN,
      WindowManager.LayoutParams.FLAG_FULLSCREEN
     )
}
于 2020-11-21T13:36:38.093 回答