Android - "setToolbarColor(int)"和"setSecondaryToolbarColor(int)"已被弃用

15

我使用这段代码打开Chrome自定义选项卡中的链接。但是setToolbarColor()setSecondaryToolbarColor()都被标记为@Deprecated。我没有找到任何替代方法。

注意: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);
1个回答

24

请使用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);

同时在 params 中添加 .setToolbarColor(ContextCompat.getColor(activity, R.color.background)),并设置 intentBuilder.setColorScheme(CustomTabsIntent.COLOR_SCHEME_DARK) - CoolMind
你有没有尝试过不使用intentBuilder.setColorScheme(CustomTabsIntent.COLOR_SCHEME_DARK)或默认的颜色方案来检查答案?在我的情况下,它会绘制白色的工具栏。 - CoolMind
1
@CoolMind CustomTabsIntent.COLOR_SCHEME_DARK 对我来说不起作用,还导致了崩溃。 - user14817809
5
@Arghadip,我在 Nexus 5(API 30) 模拟器和设备上进行了测试。我认为我们应该在设备上测试这些案例。请参见https://dev59.com/qrroa4cB1Zd3GeqPknMs#61297124。您可以尝试使用 .setDefaultColorSchemeParams(params) 吗? - CoolMind
你应该尝试使用setDefaultColorSchemeParams(params)代替setColorSchemeParams(CustomTabsIntent.COLOR_SCHEME_DARK, params)。 - Talu
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接