Fullscreen Activity in Android?

2018-12-31 04:16发布

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

25条回答
永恒的永恒
2楼-- · 2018-12-31 05:02

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.

查看更多
孤独寂梦人
3楼-- · 2018-12-31 05:03

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:

<activity android:name=".ActivityName"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen"/>

Edit:

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

<activity android:name=".ActivityName"
    android:label="@string/app_name"
    android:theme="@style/Theme.AppCompat.Light.NoActionBar"/>
查看更多
怪性笑人.
4楼-- · 2018-12-31 05:03

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:

<activity
  android:name=".ui.activity.MyActivity"
  android:theme="@style/MyTheme">
</activity>

In styles.xml:

<style name="MyTheme"  parent="your parent theme">
  <item name="android:windowNoTitle">true</item>
  <item name="android:windowFullscreen">true</item> 
</style>
查看更多
余生请多指教
5楼-- · 2018-12-31 05:03

For those using AppCompact... style.xml

 <style name="Xlogo" parent="Theme.AppCompat.DayNight.NoActionBar">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowFullscreen">true</item>

Then put the name in your manifest...

查看更多
看淡一切
6楼-- · 2018-12-31 05:04

Just paste this code into onCreate() method

requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
查看更多
像晚风撩人
7楼-- · 2018-12-31 05:06

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.

查看更多
登录 后发表回答