【问题标题】:Android - Making activity full screen with status bar on top of itAndroid - 使活动全屏并在其顶部显示状态栏
【发布时间】:2017-09-16 14:29:30
【问题描述】:

我想让我的活动全屏显示状态栏,如下图所示:

我已经在manifestactivity 标签中使用了这段代码:

'android:theme="@style/Theme.AppCompat.Light.NoActionBar"'

但我的视图不是从状态栏开始的,它看起来像这样:

如何让我的activity 看起来像第一个?

【问题讨论】:

  • 使状态栏颜色透明并在图像和顶部布局中添加 fitsystemwindow=true
  • @Utshaw,你有解决方案吗?如果是,请帮助我。需要相同的实现

标签: android android-activity android-theme android-fullscreen android-statusbar


【解决方案1】:

我知道提出这个问题的人可能已经找到了自己的解决方案,但对于仍在寻找解决方案的人来说,这是一个非常简单的解决方案,但有一点在Kitkat 之前是有限制的,所以添加了一个条件

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
                      WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
}

【讨论】:

  • @AsadMukhtar 不幸的是,它没有
  • 我如何在 pre kitkat 上实现这一点?
  • 您想要带有图标的 ContextMenu 吗? Woops 走出屏幕。你想让键盘调整大小吗?哈哈哈不!使用OnApplyWindowInsetsListener,这就像一个魅力,但它会导致其他部分出现问题。
  • 它正在工作,但底部导航视图位于导航栏后面
  • 这样做的一个问题是它还与底部的导航按钮重叠。
【解决方案2】:

将这些添加到styles.xml中的基本应用程序主题

<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>

并将以下属性添加到您的父布局:

android:fitsSystemWindows="false"

希望这会有所帮助。

【讨论】:

  • 会影响每一个活动
  • android:fitsSystemWindows=”true”,这对我不起作用。
  • 只有在我不使用 android:fitsSystemWindows="true" 时才有效
  • 应该是 android:fitsSystemWindows="false"。我发布了一个编辑,现在等待审查。否则这是正确答案。
  • using : 'true' 也覆盖了底部导航栏,
【解决方案3】:

如果你不希望你的“半透明导航”透明,这里的代码只是让状态栏透明

在您的主题中:

    <item name="android:statusBarColor">@android:color/transparent</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowTranslucentStatus">true</item>

在您的活动 onCreate:

Window window = getWindow();
WindowManager.LayoutParams winParams = window.getAttributes();
winParams.flags &= ~WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
window.setAttributes(winParams);
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);

【讨论】:

  • 节省了我的时间,我搜索了几个小时。 +1
  • 不适合我。我的内容在导航栏后面
  • 如何更改文本和图标颜色?默认为黑色
  • @bdevloper 现在不记得了。甚至不记得我在搜索什么。
  • Kotlin 代码: val window: Window = window val winParams: LayoutParams = window.attributes winParams.flags = winParams.flags 和 LayoutParams.FLAG_TRANSLUCENT_STATUS.inv() window.attributes = winParams window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE 或 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
【解决方案4】:

1。透明状态栏

window?.decorView?.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
        or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)
window.statusBarColor = Color.TRANSPARENT

2。透明状态栏和底部导航栏

    window.setFlags(
        WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
        WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
    );

3。隐藏状态栏

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
        window.insetsController?.hide(WindowInsets.Type.statusBars())
    }
    else {
        @Suppress("DEPRECATION")
        window.setFlags(
            WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN
        )
    }

4。隐藏状态栏和底部导航栏

val actionBar: ActionBar? = supportActionBar
          if (actionBar != null) actionBar.hide()
         window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LOW_PROFILE
            or View.SYSTEM_UI_FLAG_FULLSCREEN
            or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
            or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
            or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION)

把这段代码放在哪里?

   override fun onCreate(savedInstanceState: Bundle?) {

        /*  Put above code here ..... */
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_slider)
   }

注意

  • 我在 Pixel 3A 模拟器中检查了这段代码
  • 可能不支持自定义安卓操作系统
  • 设置样式&lt;style name="Theme.FullScreen" parent="Theme.MaterialComponents.DayNight.NoActionBar"&gt;

【讨论】:

  • 假设颜色操作系统和 在自定义操作系统中无法正常工作
  • 任何想法如何在保持操作栏的同时做到这一点?我想保留操作栏,但将其与状态栏合并。
【解决方案5】:

将下面的代码放在 onCreate() 中

private void setStatusBarTransparent() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        Window window = getWindow();
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);

        View decorView = window.getDecorView();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        } else {
            decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
        }
        window.setStatusBarColor(Color.TRANSPARENT);
    }
}

【讨论】:

  • 不推荐使用的答案!
  • @KishanSolanki 不,它可以在我的设备上运行。
  • @NiajMahmud 谢谢你的反应
【解决方案6】:

要实现类似的效果,请使用:

1.您的活动主题

<style name="FullScreenTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
    <item name="android:windowTranslucentNavigation">true</item>
    <item name="android:statusBarColor">@android:color/transparent</item>
    <item name="android:windowActivityTransitions">true</item>
</style>

2。无需将 android:fitsSystemWindows 属性添加到您的根布局 XML 文件中。

3。在您的活动中

为您的工具栏添加上边距。我正在使用自定义工具栏,所以我在状态栏高度的工具栏上添加了填充。

toolbar.setPadding(0, getStatusBarHeight(), 0, 0)

并且函数getStatusBarHeight()被添加到我的Utils class

fun Activity.getStatusBarHeight(): Int {
val resourceId = resources.getIdentifier("status_bar_height", "dimen", "android")
return if (resourceId > 0) resources.getDimensionPixelSize(resourceId)
else Rect().apply { window.decorView.getWindowVisibleDisplayFrame(this) }.top

}

【讨论】:

    【解决方案7】:

    这样试试

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

    希望这篇文章会有所帮助

    【讨论】:

      【解决方案8】:

      在 Kotlin 中,只需在您的 Activity 中使用以下代码,同时在您的 activity.xml 布局文件的父视图中添加 android:fitsSystemWindows="true"。 这也将在导航栏上显示内容,前提是您在 style.xml 中声明了全屏样式

      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
           window.setFlags(
                  WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
                  WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
              )
          }
      

      【讨论】:

        【解决方案9】:

        您可以在 onCreate 之后使用此代码

           setTheme(android.R.style.Theme_Black_NoTitleBar_Fullscreen);
                getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
                if (getSupportActionBar() != null) {
                    getSupportActionBar().hide();
                }
        

        您的活动将全屏显示状态栏:)

        【讨论】:

          【解决方案10】:

          @tahsinRupam 答案是正确的,并在 Kitkat 后版本上进行了测试。

          <item name="android:windowTranslucentStatus">true</item>
          <item name="android:windowTranslucentNavigation">true</item>
          

          如果您只想将它​​用于某个活动,请创建一个样式,并将其附加到您的清单样式上,如下所示。

          <activity
              android:name=".MainActivity"
              android:screenOrientation="portrait"
              android:theme="@style/Activity.Fullscreen.Theme" />
          
          <style name="Activity.Fullscreen.Theme" parent="MyMaterialTheme.Base">  
              <item name="android:windowTranslucentStatus">true</item>
              <item name="android:windowTranslucentNavigation">true</item>
          </style>
          

          点赞 @tahsinRupam

          快乐的编码干杯!

          【讨论】:

            【解决方案11】:

            将此代码放在您的 Activity onCreate 中,这将隐藏状态栏

            View decorView = getWindow().getDecorView();
            // Hide the status bar.
            int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
            decorView.setSystemUiVisibility(uiOptions);
            // Remember that you should never show the action bar if the
            // status bar is hidden, so hide that too if necessary.
            ActionBar actionBar = getActionBar();
            actionBar.hide();
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2016-05-20
              • 1970-01-01
              • 2011-09-08
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多