2025-06-14

Styled Text 介紹瞭Android的文字可以支持HTML格式,這個例子采用瞭兩種方法來顯示同一個字符串:

<string name=”styled_text”>Plain, <b>bold</b>, <i>italic</i>, <b><i>bold-italic</i></b></string>

一種是直接定義在R.layout.styled_text 中:

<TextView
android:layout_width=”match_parent” android:layout_height=”wrap_content”
android:gravity=”center_horizontal”
android:textStyle=”normal”
android:text=”@string/styled_text”/>

另外一種方法是使用代碼方法訪問資源:

[java] 
CharSequence str = getText(R.string.styled_text); 
TextView tv = (TextView)findViewById(R.id.text); 
tv.setText(str); 
CharSequence str = getText(R.string.styled_text);
TextView tv = (TextView)findViewById(R.id.text);
tv.setText(str);
註意代碼使用CharSequence 而不是使用String類型,這是為瞭能夠保持文字的Style定義。

 

作者:mapdigit
 

 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *