我是一名网络开发人员。我目前正在 Android Studio 上使用 WebView 开发 Android 应用程序,该 WebView 将我的网站作为 Android 应用程序访问。我的一个网页包含许多外部链接。我的目标是让 Android 应用程序能够像 Gmail 应用程序一样处理外部链接(也像 facebook 和 Line 一样)。
以下是 Gmail 应用程序的示例。
电子邮件包含外部链接 https://i.stack.imgur.com/ES6gk.png
单击链接,然后应用程序打开一个新活动,就像浏览器一样,无需离开 Gmail 应用程序 https://i.stack.imgur.com/uSYkC.png
知道如何制作吗?
这很简单。你必须使用Chrome 自定义标签 https://developer.chrome.com/multidevice/android/customtabs正如 Gergely 在评论中所建议的那样。下面是一些小功能代码,可以帮助您实现这一目标。
首先将此依赖项添加到您的 build.gradle(Module:app)
compile 'com.android.support:customtabs:23.4.0'
其次,将以下函数添加到您的代码中,并将字符串 URL 传递给它。
private void redirectUsingCustomTab(String url)
{
Uri uri = Uri.parse(url);
CustomTabsIntent.Builder intentBuilder = new CustomTabsIntent.Builder();
// set desired toolbar colors
intentBuilder.setToolbarColor(ContextCompat.getColor(this, R.color.colorPrimary));
intentBuilder.setSecondaryToolbarColor(ContextCompat.getColor(this, R.color.colorPrimaryDark));
// add start and exit animations if you want(optional)
/*intentBuilder.setStartAnimations(this, android.R.anim.slide_in_left, android.R.anim.slide_out_right);
intentBuilder.setExitAnimations(this, android.R.anim.slide_in_left,
android.R.anim.slide_out_right);*/
CustomTabsIntent customTabsIntent = intentBuilder.build();
customTabsIntent.launchUrl(activity, uri);
}
休息一下它会自己照顾的。由于 Chrome 自定义选项卡可以自定义,因此可以完成很多操作,例如向工具栏添加菜单。有关详细信息,您可以访问 Google 本身的官方文档here https://developer.chrome.com/multidevice/android/customtabs.
希望它能帮助您开始:)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)