本地HTML页面\\原材料\\文件夹中的WebView链接到另一个html页面 [英] Local html page \raw\ folder in webview with links to another html page
问题描述
我使用的WebView用来显示存储在\\原材料\\目录中,以便我可以很容易地定位它的HTML页面。
我使用加载页面
I'm using a WebView to display a html page which is stored in the \raw\ directory so I can easily localize it. I load the page using
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);
这工作正常。图像和CSS文件的HTML页面中使用存储在\\资产\\目录下。
which works fine. Images and css files used in the html page are stored in the \assets\ directory.
现在我想链接添加到另一个本地页面(LT; A HREF =htmlPage2.html>点击此处< / A>)在我的HTML页面。 web视图尝试加载从资产页面(这似乎是正确的行为)。不幸的是,\\资产\\文件夹没有本地化。
Now I want to add links to another local page (< a href="htmlPage2.html">Click here< /a>) in my html page. The WebView tries to load the page from assets (which seems to be the correct behavior). Unfortunately the \assets\ folder isn't localized.
我如何链接到其他地方的html页面不放弃本地化的功能?
How can I link to other local html pages without giving up the feature of localization?
谢谢!
推荐答案
第1步:可能使用更鲜明的URL模式的链接,以帮助你区分它们(例如,&LT; A HREF = /this/is/another/raw/resource/htmlPage2.html\">
)
Step #1: Probably use a more distinctive URL pattern for the links to help you distinguish them (e.g., <a href="/this/is/another/raw/resource/htmlPage2.html">
)
第二步:注册一个 WebViewClient
与的WebView
Step #2: Register a WebViewClient
with your WebView
第三步:在 shouldOverrideUrlLoading()
你的 WebViewClient
,如果您使用的网址为模式匹配这些链接(见步骤#1),通过你的负荷的资源的逻辑,并返回真正
,别的什么都不做,返回假
弹出一个Web浏览器上的网址
Step #3: In shouldOverrideUrlLoading()
of your WebViewClient
, if the URL matches the pattern you use for these links (see Step #1), go through your load-the-resource logic and return true
, else do nothing and return false
to pop up a Web browser on that URL
这篇关于本地HTML页面\\原材料\\文件夹中的WebView链接到另一个html页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!