【问题标题】:How to align contents in center into Webview如何将中心内容对齐到 Webview
【发布时间】:2014-02-13 17:15:57
【问题描述】:

我将一些内容显示到一个以<p> 开头并以</p> 标记结尾的 web 视图中。我正在通过 loadDataWithBaseURL 将内容加载到 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", "");

但它不起作用。我应该怎么做才能在中间显示这些内容?

【问题讨论】:

    标签: android html css webview


    【解决方案1】:

    终于成功了!

        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);
    

    【讨论】:

      【解决方案2】:

      尝试:

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

      【讨论】:

        猜你喜欢
        • 2020-03-01
        • 2015-10-08
        • 2018-09-09
        • 2013-04-28
        • 1970-01-01
        • 1970-01-01
        • 2019-08-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多