Android 内置webview避免外部跳转或内嵌chrome植入复杂vue项目

webview 同时支持 WebChromeClient 和 WebViewClient 但总是跳转到chrome浏览器

WebView web = (WebView)findViewById(R.id.web);
WebSettings webSettings = web.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setSupportMultipleWindows(true); // This forces ChromeClient enabled.    

web.setWebChromeClient(new WebChromeClient(){
    @Override
    public void onReceivedTitle(WebView view, String title) {
         getWindow().setTitle(title); //Set Activity tile to page title.
    }
});

web.setWebViewClient(new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return false;
    }
});

允许wenview 使用 JS 弹窗

settings.setJavaScriptEnabled(true);
settings.setJavaScriptCanOpenWindowsAutomatically(true);

SDK.Version<=21时,内嵌更好的chrome以展示VUE项目

调试URL点击跳转(http://datav.jiaminghi.com/demo/manage-desk/index.html)
例如下图大屏项目
在这里插入图片描述

  • 自带webview很难展示复杂的 VUE / typescript / javascript项目

我们可以使用Google提供的Customtabs加粗样式

在这里插入图片描述

AndroidX 版本教程

依赖

implementation "androidx.browser:browser:1.3.0"

使用

//设置颜色方案
val schemeParams = CustomTabColorSchemeParams.Builder()
                .setToolbarColor(ContextCompat.getColor(context, R.color.colorPrimary))
                .setSecondaryToolbarColor(ContextCompat.getColor(context, R.color.colorPrimaryDark))
                .build()
                
CustomTabsIntent.Builder()
                .setDefaultColorSchemeParams(schemeParams)
                .build().launchUrl(context, uri)

Android support版本

  • 开发指南

完整的示例可以查看https://github.com/GoogleChrome/custom-tabs-client。包含了定制UI、连接后台服务、处理应用和Custom Tab Activity生命周期的可复用的类。

依赖

compile 'com.android.support:customtabs:23.3.0'

使用

// 使用CustomTabsIntent.Builder配置CustomTabsIntent
// 准备完成后,调用CustomTabsIntent.Builder.build()方法创建一个CustomTabsIntent
// 并通过CustomTabsIntent.launchUrl()方法加载希望加载的url

String url = ¨https://github.com/marktony¨;
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(this, Uri.parse(url));
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

全面解读

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值