【问题标题】:Webview image fit screen in AndroidAndroid中的Webview图像适合屏幕
【发布时间】:2013-07-10 12:06:09
【问题描述】:

我将a .jpg 加载到WebView 中。我的问题是我发现了这个:Android WebView, Scaling Image to fit the screen

它对我不起作用。

这是我的代码:

Display display = getWindowManager().getDefaultDisplay();
    int width= display.getWidth();

    Toast.makeText(getApplicationContext(), ""+width, Toast.LENGTH_LONG).show();


    String html = "<html><head><title>Example</title><meta name=\"viewport\"\"content=\"width="+width+", initial-scale=0.65 \" /></head>";
    html+= "<body><img width=\""+width+"\"<img src=\""+"image.jpg"+"\" /></body></html>";

aboutText.loadDataWithBaseURL("file:///android_res/drawable/", html, "text/html","UTF-8" , null);

【问题讨论】:

标签: android html image webview screen


【解决方案1】:

这对我不起作用,因为我有高分辨率手机。

试试这个,它对我有用, webView.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);

【讨论】:

    【解决方案2】:

    您可以在将 HTML 加载到 Web 视图之前设置 img 标记的样式。请看下面的代码

    WebView content = (WebView) findViewById(R.id.webView1);
    String head = "<head> <style>img{display: inline;height: auto;max-width:   100%;}</style> <style>body {font-family: 'Roboto';  }</style></head>";
    
    content.loadDataWithBaseURL(null, head + post.getContent(), "text/html", "UTF-8", null);
    

    【讨论】:

      【解决方案3】:

      你是html图片标签错误检查下面的代码:

      Display display = getWindowManager().getDefaultDisplay();
      int width= display.getWidth();
      
      Toast.makeText(getApplicationContext(), ""+width, Toast.LENGTH_LONG).show();
      
      
      String html = "<html><head><title>Example</title><meta name=\"viewport\"\"content=\"width="+width+", initial-scale=0.65 \" /></head>";
         html+= "<body><img width=\""+width+"\" src=\""+"image.jpg"+"\" /></body></html>";
      
      aboutText.loadDataWithBaseURL("file:///android_res/drawable/", html, "text/html","UTF-8" , null);
      

      您的 html 字符串格式错误,如下代码所示:

      String html = "<html><head><title>Example</title><meta name=\"viewport\"\"content=\"width="+width+", initial-scale=0.65 \" /></head>";
      html+= "<body><img width=\""+width+"\"<img src=\""+"image.jpg"+"\" /></body></html>";
      

      格式化我的代码:

      String html = "<html><head><title>Example</title><meta name=\"viewport\"\"content=\"width="+width+", initial-scale=0.65 \" /></head>";
      html+= "<body><img width=\""+width+"\" src=\""+"image.jpg"+"\" /></body></html>";
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-10
        • 1970-01-01
        • 1970-01-01
        • 2021-05-23
        • 2011-12-10
        相关资源
        最近更新 更多