【问题标题】:load webview with image fullscreen加载带有图像全屏的 webview
【发布时间】:2017-02-03 14:46:35
【问题描述】:

我正在使用 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");
    }

}

【问题讨论】:

标签: java android


【解决方案1】:

嘿,这很简单,您只需在 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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-30
    • 2011-11-26
    • 1970-01-01
    • 2018-12-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多