问题描述
我目前正在编写一个 webview,它首先加载一个 twitter 页面(比如,NHL, http://twitter.com/nhl)如您所见,您可以找到 NHL 的推文,并且每条 NHL 推文都有另一个供用户点击的链接,例如bit.ly/ujcNZo
I am currently writing a webview, it first loads a twitter page (say, NHL, http://twitter.com/nhl) as you can see, you can find the tweet for NHL, and each NHL tweet has another link for user to click, e.g. bit.ly/ujcNZo
在我的网络视图中,如果我单击该链接(即 bit.ly/ujcNZo),我的网络视图,1 秒后,除了一个白色的 twitter 图标,什么都不显示颜色背景,它应该加载内容但它没有.
in my webview, if i click that link (i.e. bit.ly/ujcNZo), my webview, after 1 second, doesn't display anything but a twitter icon on a white color background, it should load the content but it didn't.
经过一段时间的调查,我认为这与事实有关推文中的链接(即 bit.ly/ujcNZo)实际上打开了链接在单独的窗口(弹出?)而不是当前页面链接已发布,我通过访问 NHL 的推特页面验证了这一点我的笔记本电脑上的浏览器.
after some time of investigation, i think it has to do with the fact that the link in the tweet (i.e. bit.ly/ujcNZo) actually opens the link in a separate window (pop up?) and not the current page where the link is posted, i verified this by going to NHL's twitter page on a browser in my laptop.
我的问题是,1. 有没有办法可以加载外部链接的内容 (bit.ly/ujcNZo,例如)在我当前的 web 视图中?
My Question is, 1. is there a way i can load the content of the external link (bit.ly/ ujcNZo, for instance) in my current webview?
推荐答案
你可以通过 WebViewClient 类来控制这个.只需扩展它并覆盖默认实现即可获得所需的配置,然后将其设置为当前 webview 的客户端.
You can control this through the WebViewClient class. Simply extend it and override the default implementation to get the desired configuration then set it as the client for your current webview.
活动
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// set the webViewClient to a new instance of your custom WebViewClient
webview.setWebViewClient(new WebActivityClient( this ));
}
自定义客户端
/** WebViewClient class for WebView in WebActivity */
private class WebActivityClient extends WebViewClient {
public static final String TAG = "WebActivityClient";
private Context context;
/** Constructor used to grab the context */
public WebActivityClient( Context context ) {
this.context = context;
}
/** Override to load every link within the page inside this webview instead of using
* android's default application
* #7 http://developer.android.com/resources/tutorials/views/hello-webview.html */
@Override
public boolean shouldOverrideUrlLoading( WebView view, String url ) {
view.loadUrl(url);
return true;
}
}
希望这会有所帮助!
这篇关于在当前 web 视图中打开弹出/外部站点链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!