Android-不推荐使用“setToolbarColor(int)”和“setSecondaryToolbarColor(int)”

我使用此代码打开带有 Chrome 自定义标签的链接。但它显示@DeprecatedsetToolbarColor()setSecondaryToolbarColor()。我没有找到任何可以替换的东西。

注意:Android Studio 建议“改用 setDefaultColorSchemeParams”。但还没有找到任何例子。

        Uri uri = Uri.parse(url);
        CustomTabsIntent.Builder intentBuilder = new CustomTabsIntent.Builder();
        intentBuilder.setToolbarColor(ContextCompat.getColor(activity,R.color.background));
        intentBuilder.setSecondaryToolbarColor(ContextCompat.getColor(activity,R.color.background));
        intentBuilder.setStartAnimations(activity,R.anim.slide_in_right,R.anim.slide_out_left);
        intentBuilder.setExitAnimations(activity,android.R.anim.slide_in_left,android.R.anim.slide_out_right);
        CustomTabsIntent customTabsIntent = intentBuilder.build();
        customTabsIntent.launchUrl(activity,uri);

回答

使用CustomTabColorSchemeParams来代替:参考

Uri uri = Uri.parse(url);
CustomTabsIntent.Builder intentBuilder = new CustomTabsIntent.Builder();
CustomTabColorSchemeParams params = new CustomTabColorSchemeParams.Builder()
    .setNavigationBarColor(ContextCompat.getColor(activity,R.color.background))
    .setToolbarColor(ContextCompat.getColor(activity,R.color.background))
    .setSecondaryToolbarColor(ContextCompat.getColor(activity,R.color.background))
    .build();
intentBuilder.setColorSchemeParams(CustomTabsIntent.COLOR_SCHEME_DARK, params);
intentBuilder.setStartAnimations(activity, R.anim.slide_in_right,R.anim.slide_out_left);
intentBuilder.setExitAnimations(activity,android.R.anim.slide_in_left,android.R.anim.slide_out_right);
CustomTabsIntent customTabsIntent = intentBuilder.build();
customTabsIntent.launchUrl(activity,uri);

  • @Arghadip, I tested on Nexus 5 (API 30) emulator and device. I think we should test these cases on devices. See https://stackoverflow.com/questions/61295936/cant-change-chrome-custom-tabs-navigation-bar-color-when-running-in-the-android/61297124. Could you try `.setDefaultColorSchemeParams(params)` instead?

以上是Android-不推荐使用“setToolbarColor(int)”和“setSecondaryToolbarColor(int)”的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>