我如何设置保持屏幕和全屏标志?

时间:2012-04-22 06:41:16

标签: android layoutparams android-screen android-windowmanager

在我的应用中全屏,我写了这段代码:

  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 个答案:

答案 0 :(得分:6)

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>

答案 1 :(得分:2)

在AndroidManifest.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);

答案 2 :(得分:0)

要启用全屏显示:

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

答案 3 :(得分: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
     )
}