我试图以编程方式更改StatusBar
颜色但没有成功。我正在使用MaterialDrawer库创建导航抽屉btw。
尝试在互联网上找到的不同解决方案,但没有人工作,状态栏总是显示primaryDark
以编程方式尝试:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(Color.BLUE);
}
还尝试了主题:
if (dark)
setTheme(R.style.AppTheme_Dark_NoActionBar);
else
setTheme(R.style.AppTheme_NoActionBar);
<style name="AppTheme.Dark.NoActionBar" parent="MaterialDrawerTheme">
<item name="android:colorPrimary" tools:targetApi="lollipop">#ffffff</item>
<item name="android:colorPrimaryDark" tools:targetApi="lollipop">#cfcfcf</item>
<item name="android:statusBarColor">#cfcfcf</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:windowTranslucentStatus">true</item>
<item name="android:colorAccent" tools:targetApi="lollipop">@color/md_black_1000</item>
<item name="android:windowContentTransitions" tools:targetApi="lollipop">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="drawerArrowStyle">@style/MaterialDrawer.DrawerArrowStyle.Black</item>
<item name="android:datePickerDialogTheme">@style/DatePickerDialogThemeDark</item>
<item name="android:timePickerDialogTheme">@style/DatePickerDialogThemeDark</item>
</style>
<style name="AppTheme.NoActionBar" parent="AppTheme">
<item name="windowActionBar">false</item>
<item name="android:colorAccent" tools:targetApi="lollipop">@color/primary</item>
<item name="windowNoTitle">true</item>
<item name="android:statusBarColor">@color/colorPrimaryDark</item>
<item name="android:datePickerDialogTheme">@style/DatePickerDialogThemeLight</item>
<item name="android:timePickerDialogTheme">@style/DatePickerDialogThemeLight</item>
</style>
<style name="AppTheme" parent="MaterialDrawerTheme.Light">
<item name="android:colorPrimary" tools:targetApi="lollipop">@color/colorPrimary</item>
<item name="android:colorPrimaryDark" tools:targetApi="lollipop">@color/colorPrimaryDark
</item>
<item name="android:colorAccent" tools:targetApi="lollipop">@color/md_blue_300</item>
<item name="android:windowContentTransitions" tools:targetApi="lollipop">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowDrawsSystemBarBackgrounds" tools:targetApi="lollipop">true</item>
<item name="drawerArrowStyle">@style/MaterialDrawer.DrawerArrowStyle</item>
</style>
最后尝试使用MaterialDrawer库的这段代码:
drawer.getDrawerLayout().setStatusBarBackgroundColor(color);
drawer
是DrawerBuilder
的结果。该应用程序有2种基色,具体取决于用户放入登录/注册的一些变量。这种颜色是蓝色和白色。该应用程序运行良好,除了即使我使用上面发布的代码,StatusBar仅使用primaryDark着色。我可以改变颜色的唯一模式是修改colorPrimary或将其变为颜色:
<color name="materialize_primary_dark">@color/md_red_500</color>
但它会像primaryDark那样改变颜色,我必须将其更改为白色白色模式和蓝色模式。
感谢。
答案 0 :(得分:2)
只要DrawerLayout
的根View
有fitsSystemWindows
标记,StatusBar颜色就会由此视图"scrim"
管理。
因此,在您的情况下,您可以执行以下操作来动态调整StatusBar
的颜色:
public void setStatusBarColor(@ColorInt int color){
drawer.getDrawerLayout().setStatusBarBackgroundColor(color);
}
答案 1 :(得分:1)
您不需要使用此样式的库-v21
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:windowTranslucentStatus">true</item>
</style>
抽屉布局
<FrameLayout
android:id="@+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white" />
<android.support.design.widget.NavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@color/colorPrimary"
android:fitsSystemWindows="true" /><--This is mandatory
</android.support.v4.widget.DrawerLayout>
或者您可以在Android DrawerLayout (with NavigationView) behind status bar
中提及我接受的答案