我正在使用webview,当没有互联网时,我喜欢在我的片段中显示png。 但是png并没有占据全屏。这是我的代码 -
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(....);
webview = (WebView)view.findViewById(R.id.webview1);
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebViewClient(new WebClient(getActivity()));
webview.loadUrl(url);
return view;
}
public class WebClient extends android.webkit.WebViewClient {
Context mActivity;
public WebClient(Context context){
mActivity = context;
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
view.setBackgroundColor(0);
view.loadUrl("file:///android_res/drawable/no_internet.png");
}
}
答案 0 :(得分:0)
嘿,你很简单,只需设置高度和宽度,你就可以在onReceivedError上加载url。
Display display = getWindowManager().getDefaultDisplay();
int width=display.getWidth();
String data="<html><head><title>Example</title><meta name=\"viewport\"\"content=\"width="+width+", initial-scale=0.65 \" /></head>";
data=data+"<body><center><img width=\""+width+"\" src=\""+url+"\" /></center></body></html>";
webView.loadData(data, "text/html", null);
你完成了这个问题:)
尝试使用
的另一种方法WebSettings settings = webView.getSettings();
settings.setUseWideViewPort(true);
settings.setLoadWithOverviewMode(true);