如何在 android studio 中使用 AppCompat class 更改通知栏的颜色?
How to change the color of notification bar using AppCompat class in android studio?
我想在 api 级别为 21 或更高时更改状态栏的颜色。这对我不起作用。我也在使用 supportActionBar
。我正在使用以下代码:
values/styles.xml
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<style name="MyTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
!-- All customizations that are NOT specific to a particular API-level can go here. -->
<item name="windowNoTitle">true</item>
<!-- colorPrimary is used for the default action bar background -->
<item name="colorPrimary">@color/primary</item>
<!-- colorPrimaryDark is used for the status bar -->
<item name="colorPrimaryDark">@color/primary_dark</item>
<!-- colorAccent is used as the default value for colorControlActivated
which is used to tint widgets -->
<item name="colorAccent">@color/accent</item>
<!-- You can also set colorControlNormal, colorControlActivated
colorControlHighlight and colorSwitchThumbNormal. -->
<item name="android:textColorPrimary">@color/primary_text</item>
<item name="android:textColorSecondary">@color/secondary_text</item>
</style>
<!-- Application theme. -->
<style name="MyTheme" parent="MyTheme.Base">
</style>
<style name="MyTheme.ToolBar" parent="MyTheme.Base">
<item name="android:textColorPrimary">@android:color/white</item>
<item name="android:textColorSecondary">@android:color/white</item>
</style>
<style name="MyTheme.NavMenu" parent="MyTheme.Base">
<item name="android:textColorPrimary">@color/gray</item>
</style>
<style name="MyTheme.RightMenu" parent="MyTheme.Base">
<item name="android:textColorPrimary">@color/black</item>
</style>
</resources>
在清单文件中我设置了android:theme="@style/MyTheme"
values-v21/styles.xml
<?xml version="1.0" encoding="UTF-8" ?>
<resources>
<style name="MyTheme" parent="MyTheme.Base">
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">@color/primary_dark</item>
<item name="android:windowContentTransitions">true</item>
<item name="android:windowAllowEnterTransitionOverlap">true</item>
<item name="android:windowAllowReturnTransitionOverlap">true</item>
<item name="android:windowSharedElementEnterTransition">@android:transition/move</item>
<item name="android:windowSharedElementExitTransition">@android:transition/move</item>
</style>
</resources>
这是我的颜色文件:
<resources>
<color name="primary">#3F51B5</color>
<color name="primary_dark">#303F9F</color>
<color name="primary_light">#C5CAE9</color>
<color name="accent">#FFC107</color>
<color name="primary_text">#212121</color>
<color name="secondary_text">#727272</color>
<color name="icons">#FFFFFF</color>
<color name="divider">#B6B6B6</color>
</resources>
我的样式有什么问题,为什么它不更改状态栏颜色?
改变你的
<style name="MyTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
至
<style name="MyTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:colorPrimary">@color/primary</item>
<item name="android:colorPrimaryDark">@color/primary_dark</item>
状态栏颜色可以通过colorPrimaryDark设置,
在你的 values-v21/styles.xml 添加
<resources>
<style name="AppTheme" parent="AppTheme.Base">
<item name="android:colorPrimaryDark">@color/colorPrimary</item>
<item name="android:colorPrimaryDark">@color/colorPrimayDark</item>
</style></resources>
并且 YourActivity 扩展了 AppCompatActivity
状态栏颜色是根据您的应用样式的 colorPrimaryDark
颜色值绘制的。只需将其更改为您想要操作栏的任何颜色,它就会更改。
styles.sml
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<!--This is your default actionbar color-->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
</style>
colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!--change this to whatever you need-->
<color name="colorPrimary">#03A9F4</color>
<color name="colorPrimaryDark">#0277BD</color
</resources>
因为我正在使用 Xamarin,在基础中添加这些行 activity 解决了问题:
Window window = this.Window;
window.AddFlags(WindowManagerFlags.DrawsSystemBarBackgrounds);
对于Javarefer to this question on SO。
我想在 api 级别为 21 或更高时更改状态栏的颜色。这对我不起作用。我也在使用 supportActionBar
。我正在使用以下代码:
values/styles.xml
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<style name="MyTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
!-- All customizations that are NOT specific to a particular API-level can go here. -->
<item name="windowNoTitle">true</item>
<!-- colorPrimary is used for the default action bar background -->
<item name="colorPrimary">@color/primary</item>
<!-- colorPrimaryDark is used for the status bar -->
<item name="colorPrimaryDark">@color/primary_dark</item>
<!-- colorAccent is used as the default value for colorControlActivated
which is used to tint widgets -->
<item name="colorAccent">@color/accent</item>
<!-- You can also set colorControlNormal, colorControlActivated
colorControlHighlight and colorSwitchThumbNormal. -->
<item name="android:textColorPrimary">@color/primary_text</item>
<item name="android:textColorSecondary">@color/secondary_text</item>
</style>
<!-- Application theme. -->
<style name="MyTheme" parent="MyTheme.Base">
</style>
<style name="MyTheme.ToolBar" parent="MyTheme.Base">
<item name="android:textColorPrimary">@android:color/white</item>
<item name="android:textColorSecondary">@android:color/white</item>
</style>
<style name="MyTheme.NavMenu" parent="MyTheme.Base">
<item name="android:textColorPrimary">@color/gray</item>
</style>
<style name="MyTheme.RightMenu" parent="MyTheme.Base">
<item name="android:textColorPrimary">@color/black</item>
</style>
</resources>
在清单文件中我设置了android:theme="@style/MyTheme"
values-v21/styles.xml
<?xml version="1.0" encoding="UTF-8" ?>
<resources>
<style name="MyTheme" parent="MyTheme.Base">
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">@color/primary_dark</item>
<item name="android:windowContentTransitions">true</item>
<item name="android:windowAllowEnterTransitionOverlap">true</item>
<item name="android:windowAllowReturnTransitionOverlap">true</item>
<item name="android:windowSharedElementEnterTransition">@android:transition/move</item>
<item name="android:windowSharedElementExitTransition">@android:transition/move</item>
</style>
</resources>
这是我的颜色文件:
<resources>
<color name="primary">#3F51B5</color>
<color name="primary_dark">#303F9F</color>
<color name="primary_light">#C5CAE9</color>
<color name="accent">#FFC107</color>
<color name="primary_text">#212121</color>
<color name="secondary_text">#727272</color>
<color name="icons">#FFFFFF</color>
<color name="divider">#B6B6B6</color>
</resources>
我的样式有什么问题,为什么它不更改状态栏颜色?
改变你的
<style name="MyTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
至
<style name="MyTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:colorPrimary">@color/primary</item>
<item name="android:colorPrimaryDark">@color/primary_dark</item>
状态栏颜色可以通过colorPrimaryDark设置,
在你的 values-v21/styles.xml 添加
<resources>
<style name="AppTheme" parent="AppTheme.Base">
<item name="android:colorPrimaryDark">@color/colorPrimary</item>
<item name="android:colorPrimaryDark">@color/colorPrimayDark</item>
</style></resources>
并且 YourActivity 扩展了 AppCompatActivity
状态栏颜色是根据您的应用样式的 colorPrimaryDark
颜色值绘制的。只需将其更改为您想要操作栏的任何颜色,它就会更改。
styles.sml
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<!--This is your default actionbar color-->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
</style>
colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!--change this to whatever you need-->
<color name="colorPrimary">#03A9F4</color>
<color name="colorPrimaryDark">#0277BD</color
</resources>
因为我正在使用 Xamarin,在基础中添加这些行 activity 解决了问题:
Window window = this.Window;
window.AddFlags(WindowManagerFlags.DrawsSystemBarBackgrounds);
对于Javarefer to this question on SO。