Remove unwanted White Space in WebView Android

后端 未结 11 2152
庸人自扰
庸人自扰 2020-11-30 02:05

I have started developing an App using WebView. Actually I am loading an Image with Webview (I like to use the built-in zoom controls of the class). I can successfully load

11条回答
  •  [愿得一人]
    2020-11-30 02:23

    This is an example that you can test that uses a html template, then you can replace the "body" text with whatever you would like... in your case, the html for an image. You can use the css to style the html as needed. Hope this helps you understand more how to use local assets. Post a comment if you need more help.

    One key line to not miss is

    Use this as a template: (assets/template.html)

        
    
    
        
        
        
    
    
    [CONTENT]
    
    
    

    Use css like this: (assets/style.css)

    @font-face {
      font-family: Roboto;
      src: url(file:///android_asset/fonts/Roboto.ttf);
    }
    
    html, div {
        margin: 0;
        padding: 0;
    }
    
    body {
        font-family: Roboto;
        font-size: 1.0em;
        color: rgb(61,61,61);
        text-align: left;
        position:relative;
        padding: 40px;
        background-color: #eeeeee;
    }
    

    Then in your WebView class: (your class that extends WebView)

    public void setText(CharSequence text, int color){
    
           try {
    
               InputStream is = getResources().getAssets().open("article_view.html");
               int size = is.available();
               byte[] buffer = new byte[size];
               is.read(buffer);
               is.close();
    
               loadDataWithBaseURL("file:///android_asset/",
                   new String(buffer).replace("[CONTENT]", text.toString()),
                   "text/html", "UTF-8", null);
    
           } catch (IOException e) {
               e.printStackTrace();
           }
    
    }
    

提交回复
热议问题