簡體   English   中英

Html.toHtml()未解析<small>標簽</small>

[英]Html.toHtml() is not parsing <small> tags

我試圖將Button的文本作為字符串取回,我之前對其進行了設置:

button.setText(Html.fromHtml("<sup><small>1</small></sup>/<sub><small>8</small></sub>"));

但是,從Html.toHtml()返回的String是:

<sup>1</sup>/<sub>8</sub>

如何獲得標簽?

要獲取字符串:

SpannedString spannedString = new SpannedString(button.getText());
Html.toHtml(spannedString)

toHtml()不處理<small>標記,也不會處理fromHtml()變成RelativeSizeSpan對象的任何其他標記。 您將必須編寫自己的Spanned HTML轉換程序。

TextView tv = (TextView) findViewById(R.id.text);
tv.setText(Html.fromHtml(getResources().getString(R.string.test)));

在strings.xml中定義字符串“ test”

<string name="test"><![CDATA[<sup><small>1</small></sup>/<sub><small>8</small></sub>]]></string>

在您的情況下,簡單地放置button.setText(Html.fromHtml(getResources().getString(R.string.test))); 它為我工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM