Android WebView相關屬性

來源:互聯網
上載者:User

1、設定WebView為透明:
[java] 
android:background="#00000000" 
    android:cacheColorHint="#00000000" 
    WebView.setBackgroundColor(0); 

2、WebView 顯示sd卡圖片:
[java] 
webView.loadDataWithBaseURL(null,"<img src=\"www.2cto.com" />", "text/html", "utf-8", null); 

3、WebView顯示字串
[java]
webView.loadDataWithBaseURL("fake://not/needed", s1, "text/html", "utf-8", ""); 

4、設定WebView中顯示字型的大小
[java] 
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]); 

字型大小:
[java] 
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做一下設定即可:
[java] view plaincopy
//適應全屏  39適應豎屏    57適應橫屏 
    mWebView.setInitialScale(39); 

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

6、WebView設定漸層:
[java]
android:fadingEdge="vertical" 
    android:fadingEdgeLength="20px" 
(垂直方向,上下漸層地區為20px)

7、設定WebView可觸摸放大縮小:
[java] 
mWebView.getSettings().setBuiltInZoomControls(true); 

8、WebView雙擊變大,再雙擊後變小,當手動放大後,雙擊可以恢複到原始大小,如下設定:
[java] 
webView.getSettings().setUseWideViewPort(true); 

9、幾種加速WebView載入的方法
· 提高渲染的優先順序
[java]
webView.getSettings().setRenderPriority(RenderPriority.HIGH); 
· 使用webView.getSettings().setBlockNetworkImage,把圖片載入放在最後來載入渲染
[java] 
webView.getSettings().setBlockNetworkImage(true); 

10、將字串轉換成HTML形式的檔案顯示:
[java]
//擷取的字串 
    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", ""); 
作者:huweilong1030

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.