简体   繁体   English

在Chrome自定义标签中更改状态栏颜色

[英]Change Status Bar Colour in Chrome Custom Tabs

I am using Chrome Custom Tabs to display web content in my app. 我正在使用Chrome自定义标签在我的应用中显示网页内容。 Obviously one of the main advantages of this is the ability to change UI colours in the tab. 显然,其中一个主要优点是能够在选项卡中更改UI颜色。 However, I need to change the status bar colour to something other than a darker version of the primary colour I provide. 但是,我需要将状态栏颜色更改为我提供的较暗版本的原色。

Is there anyway to do this? 反正有没有这样做?

For reference, here is my code as it stands. 作为参考,这是我的代码。

CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
builder.setToolbarColor(getResources().getColor(R.color.colorPrimary));
builder.setSecondaryToolbarColor(getResources().getColor(R.color.colorPrimary));
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(MainActivity.this, Uri.parse(url));

As you can probably guess, I want to change the status bar colour to R.color.colorPrimary rather than the automatically selected colour. 您可能猜到,我想将状态栏颜色更改为R.color.colorPrimary而不是自动选择的颜色。

Any help is greatly appreciated 任何帮助是极大的赞赏

As for now, you cannot change the status bar color when using Custom Tabs. 至于现在,使用自定义选项卡时无法更改状态栏颜色。 You can check it by yourself from the source code of CustomTabsIntent.Builder to see what you can customize or see the documentation . 您可以从CustomTabsIntent.Builder源代码CustomTabsIntent.Builder检查,以查看可以自定义的内容或查看文档

I don't try it by myself yet, but if you are targeting api >= 21 (Lollipop), I think the following code could be a work around. 我自己也没有尝试过,但是如果你的目标是api> = 21(Lollipop),我认为以下代码可能是一个解决方法。

@Override
public void onStart() {
    super.onStart();
    setStatusBarColor(R.color.colorPrimaryDark);
}

private void setStatusBarColor(int colorId) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        Window window = getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.setStatusBarColor(ContextCompat.getColor(this, colorId));
}

private void showUrl(String url) {
    setStatusBarColor(R.color.colorPrimary);
    yourCustomTabsIntent.launchUrl(this, Uri.parse(url));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM