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


当前回答

如果你不想使用主题@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>

其他回答

奇巧有一种叫做沉浸式全屏模式的技术。

例子

主题

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

谢谢你的回答@克里斯蒂安,我得到了错误

android.util.AndroidRuntimeException:必须调用requestFeature() 添加内容前

我用

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

    -----
    -----
}

下面是一个示例代码。您可以打开/关闭标志来隐藏/显示特定的部件。

public static void hideSystemUI(Activity activity) {
    View decorView = activity.getWindow().getDecorView();
    decorView.setSystemUiVisibility(
            View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    //| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    //| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
                    | View.SYSTEM_UI_FLAG_IMMERSIVE);
}

然后,重置到默认状态:

public static void showSystemUI(Activity activity) {
    View decorView = activity.getWindow().getDecorView();
    decorView.setSystemUiVisibility(
            View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}

你可以在onCreate中调用上面的函数:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.course_activity);
    UiUtils.hideSystemUI(this);
}

AndroidManifest.xml

<activity ...
          android:theme="@style/FullScreenTheme"
    >
</activity>

I.你的主应用的主题是theme . appcompat . light . darkactionbar

隐藏ActionBar / StatusBar style.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    ...
</style>

<style name="FullScreenTheme" parent="AppTheme">
    <!--this property will help hide the ActionBar-->
    <item name="windowNoTitle">true</item>
    <!--currently, I don't know why we need this property since use windowNoTitle only already help hide actionbar. I use it because it is used inside Theme.AppCompat.Light.NoActionBar (you can check Theme.AppCompat.Light.NoActionBar code). I think there are some missing case that I don't know-->
    <item name="windowActionBar">false</item>
    <!--this property is used for hiding StatusBar-->
    <item name="android:windowFullscreen">true</item>
</style>

隐藏系统导航栏

public class MainActivity extends AppCompatActivity {

    protected void onCreate(Bundle savedInstanceState) {
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
        setContentView(R.layout.activity_main)
        ...
    }
 }

2你的主应用主题是theme . appcompat . light . noactionbar

隐藏ActionBar / StatusBar style.xml

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

<style name="FullScreenTheme" parent="AppTheme">
    <!--don't need any config for hide ActionBar because our apptheme is NoActionBar-->
    <!--this property is use for hide StatusBar-->
    <item name="android:windowFullscreen">true</item> // 
</style>

隐藏系统导航栏

类似于theme。appcompat。light。darkactionbar。

演示