2014-02-13 39 views
0

我在webview中顯示了一些內容,該內容以<p>開頭並以</p>標記結尾。我正在通過loadDataWithBaseURL將內容加載到webview中,並嘗試在中間顯示內容:如何將中心的內容與Webview對齊

public static String contentPrefix = "<html><head><style type='text/css'>#content_wrapper {position: relative; margin: 0px auto;} </style></head><div id='content_wrapper'>"; 
public static String contentPostfix = "</div></html>"; 
webView.loadDataWithBaseURL("", contentPrefix + result + contentPostfix, "text/html", "UTF-8", ""); 

但它不起作用。我應該怎麼做才能在中間顯示內容?

回答

2

最後,它的工作原理!

String desiredContent = "my test html"; 
    String strBody = "<html>" 
     + "<head>" 
     + "  <style type=\"text/css\">" 
     + "  @font-face " 
     + "   { font-family: Tahoma;" 
     + "    src:url(\"~/android_asset/fonts/MyriadPro-Regular.otf\") " 
     + "   }" 
     + "  #text" 
     + "   { font-family: Tahoma;" 
     + "    font-size:14;" 
     + "    text-align: center;" 
     + "   }" 
     + "  </style>" 
     + "" 
     + "</head>" 
     + "<body dir=\"rtl\" id=\"text\">" 
     + desiredContent 
     + " </body></html> "; 

myWebView.loadDataWithBaseURL(null, strBody, "text/html", "utf-8", 
     null); 
0

嘗試:

public static String contentPrefix = "<html><head><style type=\"text/css\">p{text-align:center;}</head>";