我如何使一个活动全屏?没有通知栏。


当前回答

创建一个空活动,并在onCreate中添加两行。

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // full screen activity
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getSupportActionBar().hide();

        setContentView(R.layout.activity_main);
    }
    ...
}

其他回答

在onCreate()中的setContentView之后使用这个方法,并通过getWindow()传递Window对象。

    public void makeActivityFullScreen(Window window){
    View decorView = window.getDecorView();
    //        int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
       window.getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
    }
    decorView.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
    );
}

此代码也将适用于缺口屏幕。要检查缺口全屏,你需要安卓P,但如果你有一个缺口显示手机,然后去设置->显示设置->应用程序显示比例->选择你的应用程序->有两个安全的选项是显示和全屏,请选择全屏并运行应用程序,你可以看到缺口的全屏也没有安卓派

提示:使用getWindow().setLayout()会搞砸你的全屏显示!注意,这个方法的文档说:

设置窗口的宽度和高度布局参数… 你可以把它们改成……用于制作非全屏窗口的绝对值。

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

然后,窗口正确地调整大小,状态栏不会重新出现,不管触发这一事件。

将此添加到styles.xml中

<item name="android:windowFullscreen">true</item>

的例子,

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="android:windowFullscreen">true</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

并更改AndroidManifest文件与波纹代码

android:theme="@style/AppTheme"

的例子,

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:theme="@style/AppTheme"
    android:supportsRtl="true">

要使您的活动全屏执行以下操作:

    // add following lines before setContentView
    // to hide toolbar
                if(getSupportActionBar()!=null)
                    getSupportActionBar().hide();
    //to hide status bar
                getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);

这将隐藏工具栏和状态栏。

但在某些情况下,你可能想用透明的背景显示状态栏,在这种情况下,这样做:

// add following lines before setContentView
// to hide toolbar
if(getSupportActionBar()!=null)
   getSupportActionBar().hide();
// to make status bar transparent
getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);

一些其他的替代隐藏工具栏代替 getSupportActionBar () hide ():

通过改变应用主题的父元素来移除工具栏:

<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">

如果你想从一个活动中删除工具栏,然后去manifest,在活动标签下添加这个:android:theme="@style/ theme . appcompat.light . noactionbar "

对于kotlin爱好者,为什么不使用扩展函数呢:

对于第一种情况:

fun AppCompatActivity.makeItFullScreenStatusBarVisible(){
    supportActionBar?.hide()
    window.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS)
}

在setContentView之前调用这个:

makeItFullScreenStatusBarVisible()

第二题:

fun AppCompatActivity.makeItFullScreenStatusBarHidden(){
    supportActionBar?.hide()
    window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN)
}

在setContentView之前调用它:

makeItFullScreenStatusBarHidden()

主题

    <style name="Theme.FluidWallpaper.FullScreen" parent="@style/Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowTranslucentStatus">true</item>
    <item name="android:windowTranslucentNavigation">true</item>
    <item name="android:statusBarColor">@android:color/transparent</item>
    <item name="android:navigationBarColor">@android:color/transparent</item>
    <item name="android:windowLayoutInDisplayCutoutMode" tools:targetApi="o_mr1">shortEdges</item>
     </style>

安卓清单

   <activity android:exported="false"
        android:name=".FullScreenActivity"
        android:screenOrientation="fullSensor"
        android:theme="@style/Theme.FluidWallpaper.FullScreen"/>