Android WebView相關屬性

jopen 10年前發布 | 58K 次閱讀 Android開發 移動開發 Android WebView

1、設置WebView為透明:

android:background="#00000000" 
android:cacheColorHint="#00000000" 
WebView.setBackgroundColor(0);

2、WebView 顯示sd卡圖片:

webView.loadDataWithBaseURL(null,"<img src=\"www.2cto.com" />", "text/html", "utf-8", null);

3、WebView顯示字符串:

webView.loadDataWithBaseURL("fake://not/needed", s1, "text/html", "utf-8", "");

4、設置WebView中顯示字體的大小:

public static final TextSize[] FONT_SIZES = new TextSize[]{TextSize.SMALLER,TextSize.NORMAL,TextSize.LARGER}; 
    private WebSettings wb; 
    wb = mWebViewRightContent.getSettings(); 
    wb.setTextSize(FONT_SIZES[iFontSizeId]); 
/*字體大小:*/
public enum TextSize { 
        SMALLEST(50), 
        SMALLER(75), 
        NORMAL(100), 
        LARGER(150), 
        LARGEST(200); 
        TextSize(int size) { 
            value = size; 
        } 
        int value; 
    }

5、WebView顯示html文件時,若要達到和PC上瀏覽器顯示的效果完全一樣,只需對WebView做一下設置即可:

view plaincopy
//適應全屏  39適應豎屏    57適應橫屏 

    mWebView.setInitialScale(39); 

//注意的是:html只字體太小的話,在Android手機或開發板上顯示的就相當的小。一般6、7號字體吧!</pre>

6、WebView設置漸變:

android:fadingEdge="vertical" 
    android:fadingEdgeLength="20px" 

//(垂直方向,上下漸變區域為20px)</pre>

7、設置WebView可觸摸放大縮小:

mWebView.getSettings().setBuiltInZoomControls(true);

 8、WebView雙擊變大,再雙擊后變小,當手動放大后,雙擊可以恢復到原始大小,如下設置:

webView.getSettings().setUseWideViewPort(true);

9、幾種加速WebView加載的方法:

//· 提高渲染的優先級

webView.getSettings().setRenderPriority(RenderPriority.HIGH);

//· 使用webView.getSettings().setBlockNetworkImage,把圖片加載放在最后來加載渲染

webView.getSettings().setBlockNetworkImage(true);</pre>

10、將字符串轉換成HTML形式的文件顯示:

//獲取的字符串 
    String sDetails = cursor.getString(cursor.getColumnIndex("sChinese")); 
    //按行截取字符串,將其存放在數組中 

    String[] str = sDetails.split("\n");      String s1 = "";      //遍歷數組進行判斷,如果條件成立,就添加設定的css樣式 

    for(int i = 0;i < str.length;i ++){          if(str[i].trim().startsWith("vt.")){              str[i] = "<h3 style=\"font-size:10px; color:#000; background:#FCFCFC; padding:3px 5px;\">" + str[i] + "<h3>" + "\n";          }else if(getMark(str[i].trim())){              str[i] = "<h4 style=\"font-size:10px; color:#F60; font-weight:normal;\">" + str[i] + "</h4>" + "\n";          }else if(str[i].trim().startsWith("〖")){              str[i] = "<span style=\"color:#333; font-size:10px; color:#F60\">" + str[i] + "</span>" + "\n";          }else {              str[i] = "<p style=\"line-height:16px; font-size:10px;color:#666;\">" + str[i] + "</p>" + "\n";          }          //將修改后的字符串拼接起來 

        s1 += str[i];      }      //用WebView將字符串以HTML的形式顯示出來 

    webView.loadDataWithBaseURL("fake://not/needed", s1, "text/html", "utf-8", "");</pre>

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!