Fullscreen Activity in Android?

匿名 (未验证) 提交于 2019-12-03 01:29:01

问题:

How do I make an activity full screen? I mean without the notification bar. Any ideas?

回答1:

You can do it programatically:

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

Or you can do it via your AndroidManifest.xml file:

Edit:

If you are using AppCompatActivity then you need to set theme as below



回答2:

There's a technique called Immersive Full-Screen Mode available in KitKat.

Example



回答3:

If you don't want to use the theme @android:style/Theme.NoTitleBar.Fullscreen because you are already using a theme of you own, you can use android:windowFullscreen.

In AndroidManifest.xml:

In styles.xml:



回答4:

In AndroidManifest.xml file:

Or in Java code:

protected void onCreate(Bundle savedInstanceState){     requestWindowFeature(Window.FEATURE_NO_TITLE);     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); } 


回答5:

If your using AppCompat and ActionBarActivity, then use this

getSupportActionBar().hide();



回答6:

Be careful with

requestWindowFeature(Window.FEATURE_NO_TITLE); 

If you are using any method to set the action bar as the follow:

getSupportActionBar().setHomeButtonEnabled(true); 

It will cause a null pointer exception.



回答7:

Try this with appcompat from style.xml. It can support with all platforms.

 


回答8:

Using Android Studio (current version is 2.2.2 at moment) is very easy to add a fullscreen activity.

See the steps:

  1. Right click on your java main package > Select “New” > Select “Activity” > Then, click on “Fullscreen Activity”.

  1. Customize the activity (“Activity Name”, “Layout Name” and so on) and click “finish”.

Done!

Now you have a fullscreen activity made easily (see the java class and the activity layout to know how the things works)!



回答9:

thanks for answer @Cristian i was getting error

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

i solved this using

@Override protected void onCreate(Bundle savedInstanceState) {      requestWindowFeature(Window.FEATURE_NO_TITLE);      super.onCreate(savedInstanceState);      getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);      setContentView(R.layout.activity_login);      -----     ----- } 


回答10:

First you must to set you app theme with "NoActionBar" like below

 

Then add these lines in your fullscreen activity.

public class MainActiviy extends AppCompatActivity {     @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         requestWindowFeature(Window.FEATURE_NO_TITLE);         this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,                                   WindowManager.LayoutParams.FLAG_FULLSCREEN);         setContentView(R.layout.main);     } } 

It will hide actionbar/toolbar and also statusbar in your fullscreen activity



回答11:

For those using AppCompact... style.xml

 

Then put the name in your manifest...



回答12:

I wanted to use my own theme instead of using @android:style/Theme.NoTitleBar.Fullscreen. But it wasn't working as some post on here had mentioned, so I did some tweaking to figure it out.

In AndroidManifest.xml:

In styles.xml:

Note: in my case I had to use name="windowActionBar" instead of name="android:windowActionBar" before it worked properly. So I just used both to make sure in the case I need to port to a new Android version later.



回答13:

TIP: Using getWindow().setLayout() can screw up your full screen display! Note the documentation for this method says:

Set the width and height layout parameters of the window... you can change them to ... an absolute value to make a window that is not full-screen.

http://developer.android.com/reference/android/view/Window.html#setLayout%28int,%20int%29

For my purposes, I found that I had to use setLayout with absolute parameters to resize my full screen window correctly. Most of the time, this worked fine. It was called by an onConfigurationChanged() event. There was a hiccup, however. If the user exited the app, changed the orientation, and reentered, it would lead to firing off my code which included setLayout(). During this re-entry time window, my status bar (which was hidden by the manifest) would be made to re-appear, but at any other time setLayout() would not cause this! The solution was to add an additional setLayout() call after the one with the hard values like so:

       public static void setSize( final int width, final int height ){ //DO SOME OTHER STUFF...             instance_.getWindow().setLayout( width, height );             // Prevent status bar re-appearance             Handler delay = new Handler();             delay.postDelayed( new Runnable(){ public void run() {                 instance_.getWindow().setLayout(                     WindowManager.LayoutParams.FILL_PARENT,                     WindowManager.LayoutParams.FILL_PARENT );             }}, FILL_PARENT_ON_RESIZE_DELAY_MILLIS );         } 

The window then correctly re-sized, and the status bar did not re-appear regardless of the event which triggered this.



回答14:

show Full Immersive:

private void askForFullScreen()     {         getActivity().getWindow().getDecorView().setSystemUiVisibility(                 View.SYSTEM_UI_FLAG_LAYOUT_STABLE                         | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION                         | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN                         | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar                         | View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar                         | View.SYSTEM_UI_FLAG_IMMERSIVE);     } 

move out of full immersive mode:

 private void moveOutOfFullScreen() {         getActivity().getWindow().getDecorView().setSystemUiVisibility(                 View.SYSTEM_UI_FLAG_LAYOUT_STABLE                         | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION                         | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);     } 


回答15:

 @Override public void onConfigurationChanged(Configuration newConfig) {     super.onConfigurationChanged(newConfig);     adjustFullScreen(newConfig); }  @Override public void onWindowFocusChanged(boolean hasFocus) {     super.onWindowFocusChanged(hasFocus);     if (hasFocus) {         adjustFullScreen(getResources().getConfiguration());     } } private void adjustFullScreen(Configuration config) {     final View decorView = getWindow().getDecorView();     if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) {         decorView.setSystemUiVisibility(                 View.SYSTEM_UI_FLAG_LAYOUT_STABLE                         | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION                         | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN                         | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION                         | View.SYSTEM_UI_FLAG_FULLSCREEN                         | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);     } else {         decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE);     } } 


回答16:

 protected void onCreate(Bundle savedInstanceState) {     requestWindowFeature(Window.FEATURE_NO_TITLE);     super.onCreate(savedInstanceState);     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);     setContentView(R.layout.activity_splash_screen);     getSupportActionBar().hide();  } 


回答17:

After a lot of time with no success I came with my own solution which is quit similar with another developer. So If somebody needs her it is.My problem was that system navigation bar was not hiding after calling. Also in my case I needed landscape, so just in case comment that line and that all. First of all create style

    

This is my manifest file

This is my spalsh activity

public class Splash extends Activity { /** Duration of wait **/ private final int SPLASH_DISPLAY_LENGTH = 2000;  /** Called when the activity is first created. */ @Override public void onCreate(Bundle icicle) {     super.onCreate(icicle);     setContentView(R.layout.splash_creen);      /* New Handler to start the Menu-Activity      * and close this Splash-Screen after some seconds.*/     new Handler().postDelayed(new Runnable(){         @Override         public void run() {             /* Create an Intent that will start the Menu-Activity. */             Intent mainIntent = new Intent(Splash.this,MainActivity.class);             Splash.this.startActivity(mainIntent);             Splash.this.finish();         }     }, SPLASH_DISPLAY_LENGTH); } 

}

And this is my main full screen activity. onSystemUiVisibilityChange thi method is quit important otherwise android main navigation bar after calling will stay and not disappear anymore. Really irritating problem, but this function solves that problem.

public class MainActivity extends AppCompatActivity {

private View mContentView; @Override public void onResume(){     super.onResume();      mContentView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE             | View.SYSTEM_UI_FLAG_FULLSCREEN             | View.SYSTEM_UI_FLAG_LAYOUT_STABLE             | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY             | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION             | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);  } @Override protected void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);      setContentView(R.layout.fullscreen2);     ActionBar actionBar = getSupportActionBar();     if (actionBar != null)     {         actionBar.hide();     }     mContentView = findViewById(R.id.fullscreen_content_text);     mContentView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE             | View.SYSTEM_UI_FLAG_FULLSCREEN             | View.SYSTEM_UI_FLAG_LAYOUT_STABLE             | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY             | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION             | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);        View decorView = getWindow().getDecorView();     decorView.setOnSystemUiVisibilityChangeListener             (new View.OnSystemUiVisibilityChangeListener()             {                 @Override                 public void onSystemUiVisibilityChange(int visibility)                 {                     System.out.println("print");                      if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0)                     {                         mContentView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE                                 | View.SYSTEM_UI_FLAG_FULLSCREEN                                 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE                                 | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY                                 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION                                 | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);                     }                     else                     {                          mContentView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE                                     | View.SYSTEM_UI_FLAG_FULLSCREEN                                     | View.SYSTEM_UI_FLAG_LAYOUT_STABLE                                     | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY                                     | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION                                     | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);                          }                 }             });  } 

}

This is my splash screen layout:

  This is my fullscreen layout     

I hope this will help you



回答18:

https://developer.android.com/training/system-ui/immersive.html

Activity :

@Override public void onWindowFocusChanged(boolean hasFocus) {         super.onWindowFocusChanged(hasFocus);     if (hasFocus) {         decorView.setSystemUiVisibility(                 View.SYSTEM_UI_FLAG_LAYOUT_STABLE                 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION                 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN                 | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION                 | View.SYSTEM_UI_FLAG_FULLSCREEN                 | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);     } } 

AndroidManifests:

 


回答19:

Inside styles.xml...

 

This worked for me. Hope it'll help you.



回答20:

It worked for me.

if (Build.VERSION.SDK_INT 


回答21:

getWindow().addFlags(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN); 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!