【问题标题】:Set justification to substring of html string in android在android中为html字符串的子字符串设置对齐方式
【发布时间】:2013-04-16 06:47:14
【问题描述】:

我正在开发一个需要对齐 html 字符串的子字符串的应用程序。

问题:我使用 webview 通过以下代码设置 html 文本的对齐方式

String htmlText = "<html><body style=\"text-align:justify\"> %s </body></Html>";
WebView webView = new WebView(SubProducts.this);
webView.loadData(String.format(htmlText, description), "text/html", "utf-8");

但是当我为这样的子字符串设置相同的想法时:

String htmlText = "<html><body style=\"text-align:justify\"> %s </body></Html>";
WebView webView = new WebView(SubProducts.this);
webView.loadData(String.format(htmlText, description.substring(1,170)), "text/html", "utf-8");

在我的输出文本中 html text(

<html><body style=\"text-align:justify\"> %s </body></Html>) 

也是可见的。如何实现这一点。有人可以帮助我吗?提前致谢。

【问题讨论】:

    标签: android android-webview text-justify


    【解决方案1】:

    找到 Substring 并在 WebView 中加载,这对我来说很好。为了证明现成的代码如下所示

        public static void loadHtmlContentForAboutUs(WebView view, String appendText) {
        String s = "<html><head><style type='text/css'>body {margin:0px;color:707070;"
                + "text-align: justify;}</style></head><body>"
                + appendText
                + "</body></html>";
    
        view.loadDataWithBaseURL("", s, "text/html", "utf-8", null);
        view.setBackgroundColor(Color.TRANSPARENT);
    }
    

    【讨论】:

    • thanq sameer..但它再次做同样的事情 html 文本在 webview 中可见。
    • "description": "

      \r\n\t这是一个示例文本视图。它显示了有关我的产品的完整详细信息。

      \r\n",我需要以合理的格式显示它的子字符串。
    • 描述是否包含 HTML 标签?我认为你无法解释你的问题。您使用的是 TextView 还是 WebView?如果您想帮助帮助您的人,请投票回答
    • @Sameer..是的,我的描述包括 html 标签和使用 webview 的 iam。
    【解决方案2】:

    这解决了我的问题:

    最初我的描述包含 HTML 标签,所以我做了以下想法:

    WebView webView = new WebView(SubProducts.this);
    Spanned sub=Html.fromHtml(description);
    String s = "<html><head><style type='text/css'>body {margin:0px;color:707070;"
            + "text-align: justify;}</style></head><body>"
            + sub.subSequence(1, 150)
            + "</body></html>";
    
    webView.loadDataWithBaseURL("", s, "text/html", "utf-8", null);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-28
      • 2017-04-20
      • 1970-01-01
      相关资源
      最近更新 更多