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


当前回答

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

    // 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()

其他回答

提示:使用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">

这些答案大部分似乎都过时了。Developer.android.com建议这样做:

科特林:

private fun hideSystemBars() {
    val windowInsetsController = ViewCompat.getWindowInsetsController(window.decorView) ?: return
    windowInsetsController.systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
    windowInsetsController.hide(WindowInsetsCompat.Type.systemBars())
}

Java:

private void hideSystemBars() {
    WindowInsetsControllerCompat windowInsetsController = ViewCompat.getWindowInsetsController(getWindow().getDecorView());
    if (windowInsetsController == null) {
        return;
    }
    windowInsetsController.setSystemBarsBehavior(WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE);
    windowInsetsController.hide(WindowInsetsCompat.Type.systemBars());
}

更多信息:https://developer.android.com/training/system-ui/immersive

如果你不想使用主题@android:style/ theme . notitlebar。因为你已经在使用你自己的主题,你可以使用android:windowFullscreen。

在AndroidManifest.xml:

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

在styles.xml:

<style name="MyTheme"  parent="your parent theme">
  <item name="android:windowNoTitle">true</item>
  <item name="android:windowFullscreen">true</item> 
</style>

你可以通过编程来实现:

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"/>

编辑:

如果你正在使用AppCompatActivity,那么你需要添加新的主题

<style name="Theme.AppCompat.Light.NoActionBar.FullScreen" parent="@style/Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowActionBar">false</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowContentOverlay">@null</item>
</style>

然后使用它。

<activity android:name=".ActivityName"
    android:label="@string/app_name"
    android:theme="@style/Theme.AppCompat.Light.NoActionBar.FullScreen"/>

感谢https://stackoverflow.com/a/25365193/1646479