当前位置: 首页 > 知识库问答 >
问题:

如何在android Studio中使用AppCompat类更改通知栏的颜色?

巫马望
2023-03-14
<?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>
<?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>

我的样式有什么问题,为什么不改变状态栏的颜色??

共有1个答案

孙书
2023-03-14

状态栏颜色可以通过colorPrimaryDark设置,

在您的值-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

 类似资料:
  • 在我的一个活动中,我使用更改了工具栏的颜色。但在使用的5.0设备上,颜色是活动主题中的的颜色,所以我有两种非常不同的颜色,看起来不太好。 我意识到在5.0中可以使用,但没有这个功能。 所以我的问题是在5.0中,如何用更改状态栏颜色?

  • 该项目的最小sdk为9,最大sdk为19。 我已在res/values文件夹中创建了一个xml文件: red_actionbar.xml 至 它确实改变了我应用程序的主题,所以问题出在风格上但我不知道如何解决。

  • 晚上好,伙计们。 我有个问题。我已经心神不定好长时间了。这里我有两个AScreen和BScreen屏幕。对于AScreen,我想要一个带有黑色图标的白色状态栏。对于BScreen,一个红色的状态栏,图标为白色。如何做到这一点?? 当我在屏幕A上时,我使用Flatter_statusbarcolo状态栏为红色,我导航到屏幕B状态栏为白色,但当我返回屏幕A时,状态栏保持白色(屏幕B的颜色)

  • 问题内容: 我正在使用以下代码, 用于更改JFrame中的工具栏颜色。但这没有用。 是否可以在JFrame中更改标题栏的颜色? 问题答案: 这是不可能的。顶级JFrame由底层OS的外观控制。 您可以更改的颜色。

  • 如何在iOS 7中更改导航栏的颜色? 基本上,我想实现像推特导航条(更新推特为也就是说)这样的东西。我嵌入了一个导航条在一个的顶部。我想要的只是将导航条的颜色和顶部的实用工具条一起更改为浅蓝色。我似乎在我的中找不到一个选项。

  • 我刚刚意识到,在Lollipop下面的Android版本上,不可能通过appcompat改变状态栏的颜色。我目前正在运行Android Kitkat 4.4.2,并收到了一个应用程序的更新,以下是截图 我的问题是,他们是如何在运行比Lollipop低版本的设备上不使用appcompat而改变状态栏颜色的? 怎么可能改变颜色?