gpt4 book ai didi

android - WebView 显示不正确

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:49:59 25 4
gpt4 key购买 nike

WebView 未正确显示网站。任何帮助都会很棒!我使用的代码适用于所有其他站点。不确定是什么问题。我应该补充什么吗?在 chrome 和其他浏览器中运行良好,所以不知道该怎么做。任何帮助都会很棒!

WebView

enter image description here

Chrome

enter image description here

public class Website extends Activity {
WebView myWebView;
LinearLayout root;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.website);

myWebView = (WebView) findViewById(R.id.webView);

root = (LinearLayout) findViewById(R.id.root);

WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);

myWebView.addJavascriptInterface(new WebAppInterface(this), "Android");
myWebView.setWebViewClient(new WebViewClient());
myWebView.getSettings().setBuiltInZoomControls(true);
myWebView.getSettings().setSupportZoom(true);
myWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
myWebView.getSettings().setDomStorageEnabled(true);

myWebView.loadUrl("http://dspart.org/");

getActionBar().setDisplayHomeAsUpEnabled(true);
}

public class WebAppInterface {
Context mContext;

/** Instantiate the interface and set the context */
WebAppInterface(Context c) {
mContext = c;
}

/** Show a toast from the web page */
@JavascriptInterface
public void showToast(String toast) {
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
}

private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("http://dspart.org")) {
// This is my web site, so do not override; let my WebView
// load the page
return false;
}
// Otherwise, the link is not for a page on my site, so launch
// another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}

}
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if (myWebView.canGoBack()) {
myWebView.goBack();
} else {
root.removeView(myWebView);
myWebView.removeAllViews();
myWebView.destroy();
this.finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
root.removeView(myWebView);
myWebView.removeAllViews();
myWebView.destroy();
this.finish();
return true;
default:
return super.onOptionsItemSelected(item);

}
}
}

最佳答案

一些建议:

在配置 WebView 后移动 loadUrl()(我在评论中注意到了这一点,但无论如何它都应该在下面)。

您的页面似乎缺少样式。它们要么加载失败,要么以某种方式被 WebView 禁用。尝试添加

myWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);myWebView.getSettings().setDomStorageEnabled(true);

看起来 WebView 也被缩小了:
尝试移除
myWebView.getSettings().setUseWideViewPort(true);
myWebView.getSettings().setLoadWithOverviewMode(true);

仅供引用,您实际上并没有将 MyWebViewClient 用于任何事情,而是依赖于默认的 WebViewClient

关于android - WebView 显示不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17642285/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com