【问题标题】:Correct way to set StatusBar color on Android在 Android 上设置 StatusBar 颜色的正确方法
【发布时间】:2020-04-15 06:22:49
【问题描述】:

我制作了一个带有默认主题 (Ionic 5) 的空白启动应用程序。

对于 Android,这就是它的外观。这看起来很奇怪,因为我希望将原色的较暗版本用作状态栏的颜色。

状态栏为黑色。将状态栏的颜色设置为其他颜色的正确方法是什么?

我尝试了以下方法:

  1. 状态栏 this.statusBar.backgroundColorByHexString('#ffffff');
  2. 首选项中的状态栏颜色 <preference name="StatusBarBackgroundColor" value="#457492" />

但这些都不起作用。目前,我已经通过 Android 项目的 MainActivity 来执行此操作。

getWindow().setStatusBarColor(ContextCompat.getColor(this, R.color.status_bar_color));

app.component.ts

export class AppComponent {
  navigate: any;
  constructor(
    private platform: Platform,
    private splashScreen: SplashScreen,
    private statusBar: StatusBar
  ) {
    this.initializeApp();
  }

  initializeApp() {
    this.platform.ready().then(() => {
      // this.statusBar.styleDefault();
      this.splashScreen.hide();
      this.statusBar.backgroundColorByHexString('#ff371d');
      this.sideMenu();
    });
  }
}

【问题讨论】:

  • 第一个是正确的,但只需删除 this.statusbar.deafultStyle() ,因为它可能与此方法发生冲突。
  • 还是不行。
  • 在您的问题中添加您的 app.compoent.ys。
  • this.statusBar.backgroundColorByName("red");试试这个,虽然 backgrounscolorbyhexstring 应该可以正常工作,但让我们看看错误在哪里,对于 statusbar.defaultstyle,它的状态栏中图标或文本的颜色为黑色或白色
  • 尝试:this.statusBar.styleDefault(); this.statusBar.styleBlackTranslucent(); 侧边initializeApp()

标签: android ionic-framework ionic-native ionic5


【解决方案1】:

您也可以在父主题中设置状态栏颜色。

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
       ...
        <item name="android:statusBarColor">#ffffff</item>
       ...

    </style>

【讨论】:

  • 仅此一项是行不通的。这只会在初始屏幕期间更改状态栏颜色,之后不会。
  • 我已经在我的应用程序中尝试过了,它可以工作。请检查其他屏幕getWindow().setStatusBarColor(ContextCompat.getColor(this, R.color.status_bar_color)); 将覆盖样式效果。
  • 即使我删除了这个 getWindow()... 电话也不起作用。
【解决方案2】:

platform.ready()之后颜色变了吗?

this.platform.ready().then(() => {
   this.statusBar.backgroundColorByHexString('#A9C23F');
}

我没有更改配置中的任何内容,这对我有用

【讨论】:

    【解决方案3】:

    在 style.xml 中添加这一行:

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

    或

    item name="colorPrimaryDark">#0000</item>
    

    【讨论】:

      猜你喜欢
      • 2015-12-31
      • 1970-01-01
      • 2015-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-13
      • 2021-08-09
      • 1970-01-01
      相关资源
      最近更新 更多