-->

Android Fullscreen Not Working: Gingerbread

2019-08-26 10:51发布

问题:

I am trying to make a fullscreen activity, but it doesn't seem to work in GingerBread, here is my code

 @SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_eyes);
   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH){
    ActionBar actionBar = getActionBar();
    actionBar.hide();

}

   if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD){
       requestWindowFeature(Window.FEATURE_NO_TITLE);
       getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
   }

In Gingerbread there is still a title.

Does anyone know how to fix this?

回答1:

First of all you should set the activity as full screen in the manifest. In the desired activity, add this code

android:theme="@android:style/Theme.NoTitleBar.Fullscreen"

and your onCreate should be like this

if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD){
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
}

setContentView(R.layout.main);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH){
    ActionBar actionBar = getActionBar();
    actionBar.hide();

}

This might solve the problem.



回答2:

Remember that you must call requestWindowFeature before call setContentView

Try this to make a full screen activity by java:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

and in Manifest

android:theme="@android:style/Theme.Translucent.NoTitleBar"


回答3:

requestWindowFeature has to be called before super.onCreate. Like so:

@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {

    if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD){
        requestWindowFeature(Window.FEATURE_NO_TITLE);
    }

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_eyes);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH){
        ActionBar actionBar = getActionBar();
        actionBar.hide();
    }

    if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD){
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }
    // ...

If this doesn't work, move the getWindow().setFlags(...) call to the top as well.



回答4:

Unless you have a reason to not setting it in your manifest, add this attribute to your tag in the Manifest:

<application
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
    .....
>

And I believe you wouldn't need the if-conditions code in the onCreate().



回答5:

isn't just android:theme ="@android:style/Theme.NoTitleBar.Fullscreen" both hides the titlebar and the notification area?

it equals to:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);

I learn this from https://github.com/pocorall/scaloid-apidemos/blob/master/src/main/java/com/example/android/apis/graphics/CameraPreview.java