在 Chrome 个自定义选项卡中更改状态栏颜色

Change Status Bar Colour in Chrome Custom Tabs

我正在使用 Chrome 自定义标签在我的应用程序中显示网络内容。显然,这样做的主要优点之一是能够在选项卡中更改 UI 颜色。但是,我需要将状态栏颜色更改为我提供的主色的深色版本以外的颜色。

有办法吗?

作为参考,这是我的代码。

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));

您可能猜到了,我想将状态栏颜色更改为 R.color.colorPrimary 而不是自动选择的颜色。

非常感谢任何帮助

目前,您无法在使用自定义标签时更改状态栏颜色。您可以从source code of CustomTabsIntent.Builder to see what you can customize or see the documentation.

中自行查看

我还没有自己尝试,但如果你的目标是 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));
}