如何将背景颜色设置为透明状态栏

时间:2016-07-05 07:52:40

标签: android-layout colors window styles statusbar

是否可以通过编程方式更改透明状态栏的背景颜色?

我已使用以下代码将状态栏设置为透明,因此我也可以将状态栏中的导航抽屉放在导航抽屉的状态栏中。

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

状态栏采用背景颜色

<item name="colorPrimaryDark">@color/primary_dark</item>

是否可以通过编程方式更改此颜色并使状态栏仍然透明?

我尝试使用

window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(statusBarColor);

但状态栏上有一个丑陋的黑色稀松布,我不想要。

还有其他方法可以通过编程方式实现此目的吗?我需要能够在运行时更改颜色。此外,每种颜色的样式也不同,因为用户应该能够选择颜色。

编辑:我的意思是使用setStatusBarColor(int);更改颜色时状态栏与导航抽屉重叠。我仍然希望导航抽屉与状态栏重叠。

enter image description here

1 个答案:

答案 0 :(得分:0)

使用这种方式。

// Check if the version of Android is Lollipop or higher
if (Build.VERSION.SDK_INT >= 21) {

    // Set the status bar to dark-semi-transparentish
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
            WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);


}

UpDate:

而不是

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

使用以下内容:

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

UpDate2:

<强> styles.xml

 <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="android:statusBarColor">@android:color/transparent</item>
 </style>

<强> colors.xml

 <color name="colorPrimary">#0072BA</color>
 <color name="colorPrimaryDark">#004F80</color>
 <color name="colorAccent">#005D96</color>

输出

没有抽屉

enter image description here

带抽屉

enter image description here