【问题标题】:Local html page \raw\ folder in webview with links to another html pagewebview 中的本地 html 页面 \raw\ 文件夹,其中包含指向另一个 html 页面的链接
【发布时间】:2013-01-10 23:31:06
【问题描述】:

我正在使用 WebView 来显示一个 html 页面,该页面存储在 \raw\ 目录中,因此我可以轻松地对其进行本地化。 我使用

加载页面
 InputStream inputStream = getResources().openRawResource(R.id.htmlPage1);
 StringWriter writer = new StringWriter();
 try {
     IOUtils.copy(inputStream, writer);
 } catch (IOException e) {
     e.printStackTrace();
 }
 String x = writer.toString();
 WebView w = (WebView) myFragmentView.findViewById(R.id.webView);
 w.loadDataWithBaseURL("file:///android_asset/", x, "text/html", "utf-8", null);

效果很好。 html页面使用的图片和css文件存放在\assets\目录下。

现在我想在我的 html 页面中添加指向另一个本地页面(单击此处)的链接。 WebView 尝试从资产加载页面(这似乎是正确的行为)。不幸的是,\assets\ 文件夹没有本地化。

如何在不放弃本地化功能的情况下链接到其他本地html页面?

谢谢!

【问题讨论】:

    标签: android android-webview


    【解决方案1】:

    第 1 步:可能对链接使用更独特的 URL 模式以帮助您区分它们(例如,<a href="/this/is/another/raw/resource/htmlPage2.html">

    第 2 步:使用您的 WebView 注册 WebViewClient

    第 3 步:在 WebViewClientshouldOverrideUrlLoading() 中,如果 URL 与您用于这些链接的模式匹配(请参阅第 1 步),请检查您的加载资源逻辑并返回 true,否则什么都不做并返回false 以在该 URL 上弹出一个 Web 浏览器

    【讨论】:

      猜你喜欢
      • 2016-07-05
      • 2019-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-13
      • 2013-01-27
      • 2013-03-18
      相关资源
      最近更新 更多