透明状态栏android

时间:2017-09-07 06:31:36

标签: android android-statusbar android-navigation-bar

我试过

statusBarColor 设置为透明,但会留下阴影

windowTranslucentStatus 设置为 true ,但会留下阴影

将上述属性与fitsSystemWindow混合并匹配......没有成功

执行以下操作可实现完全透明状态的目标......但也可使导航栏完全透明

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        Window w = getWindow(); // in Activity's onCreate() for instance
        w.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
    }

如何在不触摸导航栏的情况下仅使状态栏100%透明

minSdk:21

目标:26

编译26

Android studio 3.0预览版(最新截至9/6/17)

3 个答案:

答案 0 :(得分:2)

我认为你不能使状态栏100%透明,因为活动有自己的窗口背景(默认背景为白色,你可以使用getWindow()。setBackgroundDrawableResource(int resID)或getWindow()设置窗口背景。 setBackgroundDrawable(Drawable drawable))

  

您可以将颜色设置为状态栏,并在以下代码中显示的setContentView()之后以十六进制形式设置颜色不透明度。

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        getWindow().setStatusBarColor(Color.parseColor("#00FFFFFF"));
    }

//Here "#00FFFFFF" means opacity is 0% (first two digits 00) and colour is white (next FFFFFF character).
  

或者在styles.xml中(仅限Lollipop版本。兼容性可能会受到影响)

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

要设置不透明度,请参阅this链接

答案 1 :(得分:0)

在状态栏中使用此十六进制颜色#00000000

答案 2 :(得分:0)

我在相同的条件下做了同样的事情

你对FLAG_LAYOUT_NO_LIMITS是正确的,但这是方式(kotlin中的代码)

//onCreate
window.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS)
toolbar.setPadding(0, getStatusBarHeight(), 0, 0)

getStatusBarHeight:

private fun getStatusBarHeight(): Int {
    var result = 0
    val resourceId = resources.getIdentifier("status_bar_height", "dimen", "android")
    if (resourceId > 0) {
        result = resources.getDimensionPixelSize(resourceId)
    }
    return result
}

以及如何返回导航栏的方法 - 将其放入您的样式中:

    <item name="android:windowTranslucentStatus">false</item>
    <item name="android:windowDrawsSystemBarBackgrounds">false</item>
相关问题