[英]Set TextView text from html-formatted string resource in XML
我的strings.xml
中有一些固定字符串,例如:
<resources>
<string name="somestring">
<B>Title</B><BR/>
Content
</string>
</resources>
在我的布局中,我有一個TextView
,我想用 html 格式的字符串填充它。
<TextView android:id="@+id/formattedtext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/htmlstring"/>
如果我這樣做, formattedtext
文本的內容只是剝離任何 html 標簽的somestring
的內容,因此未格式化。
我知道可以通過編程方式設置格式化文本
.setText(Html.fromHtml(somestring));
因為我在我的程序的其他部分使用它,它按預期工作。
要調用這個 function 我需要一個Activity
,但目前我的布局只是一個簡單的或多或少的 static 視圖在普通 XML 中,我寧願保持這種狀態,以避免我創建Activity
的開銷只是為了設置一些文本。
我是否忽略了一些明顯的東西? 難道根本不可能嗎? 歡迎任何幫助或解決方法!
編輯:剛剛嘗試了一些事情,似乎 xml 中的 HTML 格式有一些限制:
標簽必須小寫
這里提到的一些標簽不起作用,例如<br/>
(可以使用\n
代替)
萬一有人發現這個,有一個更好的替代品沒有記錄(我搜索了幾個小時后絆倒了它,最后在Android SDK本身的bug列表中找到它)。 您可以在strings.xml中原始的HTML,只要你把它包裝
<![CDATA[ ...raw html... ]]>
例:
<string name="nice_html">
<![CDATA[
<p>This is a html-formatted string with <b>bold</b> and <i>italic</i> text</p>
<p>This is another paragraph of the same string.</p>
]]>
</string>
然后,在您的代碼中:
TextView foo = (TextView)findViewById(R.id.foo);
foo.setText(Html.fromHtml(getString(R.string.nice_html)));
恕我直言,這是幾個數量級更好的工作:-)
由於這里的最佳答案是暗示出錯 (或者至少過於復雜),我覺得應該更新,盡管問題已經很久了:
在Android中使用String資源時,您只需從Java代碼調用getString(...)
或在布局XML中使用android:text="@string/..."
。
即使您想在字符串中使用HTML標記,也不必進行大量更改:
您需要在String資源中轉義的唯一字符是:
"
變成\\"
'
變成\\'
&
變成&
或者&
這意味着您可以添加HTML標記而無需轉義標記:
<string name="my_string"><b>Hello World!</b> This is an example.</string>
但是,可以肯定的是,您應該只使用文檔中列出的<b>
, <i>
和<u>
。
如果你想使用XML中的HTML字符串,繼續使用android:text="@string/..."
,它將正常工作。
唯一的區別是,如果你想使用Java代碼中的 HTML字符串,你現在必須使用getText(...)
而不是getString(...)
,因為前者保留了樣式而后者只是剝離它關了。
就這么簡單。 沒有CDATA,沒有Html.fromHtml(...)
。
您只需要Html.fromHtml(...)
如果你沒有編碼的HTML標記的特殊字符。 然后將它與getString(...)
一起使用。 如果要將String傳遞給String.format(...)
則可能需String.format(...)
。
這些都在文檔中描述。
編輯:
getText(...)
與未轉義的HTML(正如我所提議的)或CDATA
部分和Html.fromHtml(...)
之間沒有區別。
請參閱以下圖表以進行比較:
轉義HTML標記...
<resources>
<string name="somestring">
<B>Title</B><BR/>
Content
</string>
</resources>
Android沒有指定資源字符串類型的規范(例如text / plain或text / html)。 但是,有一種解決方法可以讓開發人員在XML文件中指定它。
一旦定義了這些,就可以用xml文件中的HTML表達自己,而無需再次調用setText(Html.fromHtml(...))。 我很驚訝這種方法不是API的一部分。
此解決方案適用於Android工作室模擬器將文本顯示為呈現HTML的程度。
<resources>
<string name="app_name">TextViewEx</string>
<string name="string_with_html"><![CDATA[
<em>Hello</em> <strong>World</strong>!
]]></string>
</resources>
聲明自定義屬性命名空間,並添加android_ex:isHtml屬性。 還可以使用TextView的子類。
<RelativeLayout
...
xmlns:android_ex="http://schemas.android.com/apk/res-auto"
...>
<tv.twelvetone.samples.textviewex.TextViewEx
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/string_with_html"
android_ex:isHtml="true"
/>
</RelativeLayout>
<resources>
<declare-styleable name="TextViewEx">
<attr name="isHtml" format="boolean"/>
<attr name="android:text" />
</declare-styleable>
</resources>
package tv.twelvetone.samples.textviewex;
import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.Nullable;
import android.text.Html;
import android.util.AttributeSet;
import android.widget.TextView;
public TextViewEx(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TextViewEx, 0, 0);
try {
boolean isHtml = a.getBoolean(R.styleable.TextViewEx_isHtml, false);
if (isHtml) {
String text = a.getString(R.styleable.TextViewEx_android_text);
if (text != null) {
setText(Html.fromHtml(text));
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
a.recycle();
}
}
}
最近更新:
Html.fromHtml(string);
//在Android N版本之后棄用..
以下代碼支持android N及以上版本...
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
textView.setText(Html.fromHtml(yourHtmlString,Html.FROM_HTML_MODE_LEGACY));
}
else
{
textView.setText(Html.fromHtml(yourHtmlString));
}
String termsOfCondition="<font color=#cc0029>Terms of Use </font>";
String commma="<font color=#000000>, </font>";
String privacyPolicy="<font color=#cc0029>Privacy Policy </font>";
Spanned text=Html.fromHtml("I am of legal age and I have read, understood, agreed and accepted the "+termsOfCondition+commma+privacyPolicy);
secondCheckBox.setText(text);
我有另一種情況,當我從服務器收到字符串HTML時,我沒有機會將CDATA放入xml。
這是我從服務器得到的:
<p>The quick brown <br />
fox jumps <br />
over the lazy dog<br />
</p>
它似乎更復雜,但解決方案更簡單。
private TextView textView;
protected void onCreate(Bundle savedInstanceState) {
.....
textView = (TextView) findViewById(R.id.text); //need to define in your layout
String htmlFromServer = getHTMLContentFromAServer();
textView.setText(Html.fromHtml(htmlFromServer).toString());
}
希望能幫助到你!
靈
請遵循此代碼
Kotlin
var stringvalue = "Your Sting"
yourTextVew.text = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Html.fromHtml(stringvalue, Html.FROM_HTML_MODE_COMPACT)
} else {
Html.fromHtml(stringvalue)
}
Java
String stringvalue = "Your String";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
yourTextVew.setText(Html.fromHtml(stringvalue, Html.FROM_HTML_MODE_COMPACT))
} else {
yourTextVew.setText( Html.fromHtml(stringvalue))
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.