【问题标题】:Can I set FLAG_LAYOUT_NO_LIMITS only for status bar?我可以只为状态栏设置 FLAG_LAYOUT_NO_LIMITS 吗?
【发布时间】:2016-01-25 15:27:03
【问题描述】:

我需要制作透明状态栏。我正在使用getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS),它是我想要的状态栏。但它也会影响导航栏:它变得透明,getWindow().setNavigationBarColor(Color.BLACK) 什么也不做。

有没有办法只做透明状态栏而不做导航栏?

【问题讨论】:

  • 您找到解决方案了吗?
  • 我也对解决方案感兴趣,到目前为止我什么也没找到......
  • 您能否发布您当前和预期行为的图片?

标签: android android-5.0-lollipop android-statusbar


【解决方案1】:

这对我有用

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

styles.xml

<style name="TranslucentStatusBar" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowTranslucentStatus">true</item>
</style>

v21\styles.xml

<style name="TranslucentStatusBar" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowDrawsSystemBarBackgrounds">false</item>
    <item name="android:windowTranslucentStatus">true</item>
</style>

状态栏将透明或半透明,导航栏不会

希望这会有所帮助!

【讨论】:

  • 完美简洁的答案。荣誉
  • 终于!准确而原始的答案。
  • 完美答案。
  • 防止滚动视图滚动
  • 这不适用于三星 SM-T295(Android 10,One UI 2.1)。导航栏显示在内容的顶部。
【解决方案2】:

使用mikepenz's comment

我在下面的确切工作代码(转换为 kotlin)。

// at AppCompatActivity, min SDK is 16, I tested api 25
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    if (Build.VERSION.SDK_INT >= 19 && Build.VERSION.SDK_INT < 21) {
        window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
    }
    if (Build.VERSION.SDK_INT >= 19) {
        window.decorView.systemUiVisibility =
            View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
    }
    if (Build.VERSION.SDK_INT >= 21) {
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
        window.statusBarColor = Color.TRANSPARENT
    }

    setContentView(R.layout.activity_main)
}

【讨论】:

  • window.decorView.systemUiVisibility = window.decorView.systemUiVisibility or View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN 更好(我需要这个来保持状态栏图标黑色而不是白色)
【解决方案3】:

向下滚动以查看最终结果的样子

首先,像这样定义你的styles.xml——

styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
</style>

请勿添加以下行

&lt;item name="android:windowTranslucentStatus"&gt;true&lt;/item&gt;

当软键盘显示在带有EditText 的对话框上时,添加上面的行不会向上移动布局

然后在 v21 这样的 v23 样式中覆盖此样式-

v21/styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
     <item name="android:windowDrawsSystemBarBackgrounds">false</item>
</style>

v23/styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
     <item name="android:windowDrawsSystemBarBackgrounds">false</item>
</style>

活动代码 - Kotlin

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    window.setFlags(
            LayoutParams.FLAG_LAYOUT_NO_LIMITS,
            LayoutParams.FLAG_LAYOUT_NO_LIMITS
    )
    setContentView(R.layout.YOUR_LAYOUT_RESOURCE_ID)
    .
    .
.
}

活动代码 - Java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(
            LayoutParams.FLAG_LAYOUT_NO_LIMITS,
            LayoutParams.FLAG_LAYOUT_NO_LIMITS
    )
    setContentView(R.layout.YOUR_LAYOUT_RESOURCE_ID)
    .
    .
    .
}

最终结果

【讨论】:

  • 它对我有用。此外,我所要做的就是根据状态栏调整我的布局顶部。非常感谢:)
  • @NikunjPatel 它对我(和其他 4 人)有用。您能否将您的代码作为一个不适合您的问题发布并在此处发送链接。也许我可以帮忙。
  • 几乎设备运行良好,但它不适用于运行 Android 10 和 One UI 2.0 的三星 Note 10+ 导航栏仍然透明并与应用程序的内容重叠。
  • FLAG_LAYOUT_NO_LIMITS 隐藏操作栏
【解决方案4】:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

【讨论】:

  • 这会隐藏我的工具栏吗?
【解决方案5】:
fun showTransparentStatusbar() {
        activity!!.window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN)
    }


    fun removeStatusbarFlags() {
        activity!!.window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
    }

【讨论】:

  • 感谢只有这个对我有用。干净和精确。无需额外信息
  • 它使状态栏消失
【解决方案6】:

试试这个,不会后悔的

window.decorView.systemUiVisibility = (
     View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
     View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
)

【讨论】:

  • 你是对的..我试过了,没有后悔
  • 这会隐藏我的工具栏吗?
【解决方案7】:

你可以这样使用 隐藏状态栏和导航栏

WindowManager.LayoutParams attributes = getWindow().getAttributes();
    attributes.flags |= WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;
    getWindow().setAttributes(attributes);

为了再次显示导航栏,请使用此

   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION, WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

    }

颜色对我来说是灰色的,也许你可以强制它为你的原色

【讨论】:

    【解决方案8】:
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    

    上面的标志对我有用。导航按钮可见,状态栏和操作栏隐藏。

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
    

    不工作。测试设备关系 5。

    【讨论】:

    • 你解决了吗?
    • 那是 3 年前 ) 是的。 WindowManager.LayoutParams.FLAG_FULLSCREEN 适用于 nexus 5。
    【解决方案9】:

    我找到了解决方案,兄弟

    <style name="transparent" parent="Theme.AppCompat.Light.NoActionBar">//AppCompat is the key; You can choose any other theme than the light-theme, but stick to AppCompat
        <item name="android:windowTranslucentStatus">false</item>
        <item name="android:windowDrawsSystemBarBackgrounds">false</item>
        //Other styling(optional)
    </style>
    

    然后像这样将此透明主题应用于您的活动清单

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

    【讨论】:

    • 此解决方案按描述完成工作。导航栏不透明,下方没有内容。
    • 您使用的是哪个 Android,因为它适用于较旧的 android 6 或 7
    【解决方案10】:

    对于所有对变通办法/hacks 感兴趣的人,因为涉及此问题的 Android API 实在是太糟糕了。不要使用任何系统窗口标志,将负边距设置为布局的根,然后使用setStatusBarColor 使您的StatusBar 透明。

    statusBarHeight = getResources().getDimensionPixelSize(R.dimen.margin_24dp);
    int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
    if (resourceId != 0) {
        statusBarHeight = getResources().getDimensionPixelSize(resourceId);
    }
    
    FrameLayout.LayoutParams rootParams = (FrameLayout.LayoutParams) binding.getRoot().getLayoutParams();
    rootParams.topMargin = -statusBarHeight;
    binding.getRoot().setLayoutParams(rootParams);
    
    getWindow().setStatusBarColor(getResources().getColor(R.color.transparent));
    

    binding.getRoot() 当然是你布局的根

    结果是

    【讨论】:

    • 你只是调整了视图的边距,如果我有工具栏怎么办?
    • 你是什么意思?我将边距设置为布局的根组件。在那个布局中,我也有工具栏
    【解决方案11】:

    是的,您可以在 Style.xml 中使用此代码

    <style name="transparent" parent="Theme.AppCompat.Light">//AppCompat is the key; You can choose any other theme than the light-theme, but stick to AppCompat
    
        <item name="colorPrimaryDark">#00000000</item>//THIS is the important part.
        //Other styling(optional)
    </style>
    

    然后要将它应用到您的布局中,只需在根布局(视图)中添加以下行:

    android:theme="@style/transparent"
    

    【讨论】:

      【解决方案12】:
      getWindow().setFlags(
          WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
          WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
      )
      
      <style name="TranslucentStatusBar" parent="Theme.AppCompat.Light.NoActionBar">
          <item name="android:windowTranslucentStatus">true</item>
      </style>
      

      【讨论】:

        【解决方案13】:

        这适用于三星 S10+ (Pie)、Oppo F7 和 Oppo F5S (Oreos):

        getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_OVERSCAN);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        

        如原发帖人所问,应用程序区域在顶部扩展并覆盖状态栏(时钟所在的位置)。但安卓导航(虚拟)按钮仍位于屏幕底部,而应用位于安卓按钮之上。

        【讨论】:

        • 它不会使状态栏透明。它只是隐藏了状态栏。
        猜你喜欢
        • 2019-04-18
        • 2021-12-19
        • 1970-01-01
        • 2014-04-17
        • 1970-01-01
        • 2021-01-06
        • 2013-07-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多