TextView支持的HTML標籤及其餘

Textview並不支持全部的html標籤。若是更復雜的,能夠直接使用webview組件。


Android TextView 支持的HTML標籤

There is a lovely method on the android.text.Html  class, fromHtml() , that converts HTML into a Spannable  for use with a TextView .
However, the documentation does not stipulate what HTML tags are supported, which makes this method a bit hit-or-miss. More importantly, it means that you cannot rely on what it will support from release to release.
I have filed  an issue   requesting that Google formally document what it intends to support. In the interim, from a quick look at the source code, here’s what seems to be supported as of Android 2.1:
 
  • <a href="...">  定義連接內容
  • <b>  定義粗體文字   b 是blod的縮寫
  • <big>  定義大字體的文字
  • <blockquote>  引用塊標籤 
    • 屬性:
      • Common  -- 通常屬性
      • cite  -- 被引用內容的URI
  • <br>   定義換行
  • <cite>   表示引用的URI
  • <dfn>   定義標籤  dfn 是defining instance的縮寫
  • <div align="...">
  • <em>  強調標籤  em 是emphasis的縮寫
  • <font size="..." color="..." face="...">
  • <h1>
  • <h2>
  • <h3>
  • <h4>
  • <h5>
  • <h6>
  • <i>   定義斜體文字
  • <img src="...">
  • <p>     段落標籤,裏面能夠加入文字,列表,表格等
  • <small>  定義小字體的文字
  • <strike>   定義刪除線樣式的文字   不符合標準網頁設計的理念,不同意使用.   strike是strikethrough的縮寫
  • <strong>   重點強調標籤
  • <sub>   下標標籤   sub 是subscript的縮寫
  • <sup>   上標標籤   sup 是superscript的縮寫
  • <tt>   定義monospaced字體的文字  不同意使用.  此標籤對中文沒意義  tt是teletype or monospaced text style的意思
  • <u>   定義帶有下劃線的文字  u是underlined text style的意思
1、在xml文件中使用android:textStyle=」bold」
 
2、可是不能將中文設置成粗體,將中文設置成粗體的方法是: 

TextView tv =  ( TextView) findViewById( R. id . TextView01) ;  
TextPaint tp =  tv. getPaint ( ) ;  
tp. setFakeBoldText( true) ;


Selecting, Highlighting, or Styling Portions of Text

You can highlight or style the formatting of strings or substrings of text in a TextView object. There are two ways to do this:

If you use a string resource, you can add some simple styling, such as bold or italic using HTML notation. The currently supported tags are: B (bold), I (italic), U (underline), TT (monospace), BIG, SMALL, SUP (superscript), SUB (subscript), and STRIKE (strikethrough). So, for example, in res/values/strings.xml you could declare this:
<resource>
    <string>id="@+id/styled_welcome_message">We are <b><i>so</i></b> glad to see you.</string>
</resources>
To style text on the fly, or to add highlighting or more complex styling, you must use the Spannable object as described next.
To style text on the fly, you must make sure the TextView is using Spannable storage for the text (this will always be true if the TextView is an EditText), retrieve its text with getText(), and call setSpan(Object, int, int, int), passing in a new style class from the android.text.style package and the selection range.

The following code snippet demonstrates creating a string with a highlighted section, italic section, and bold section, and adding it to an EditText object.

// Get our EditText object.
EditText vw = (EditText)findViewById(R.id.text);

// Set the EditText's text.
vw.setText("Italic, highlighted, bold.");

// If this were just a TextView, we could do:
// vw.setText("Italic, highlighted, bold.", TextView.BufferType.SPANNABLE);
// to force it to use Spannable storage so styles can be attached.
// Or we could specify that in the XML.

// Get the EditText's internal text storage
Spannable str = vw.getText();

// Create our span sections, and assign a format to each.
str.setSpan(new StyleSpan(android.graphics.Typeface.ITALIC), 0, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
str.setSpan(new BackgroundColorSpan(0xFFFFFF00), 8, 19, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
str.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 21, str.length() - 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

I sometimes have the case to arrange the p_w_picpath next to the characters.We can do it by putting TextView and ImageView into Layout.But today I introduce the other way using only TextView.The following sample code is how to show the p_w_picpath next to text.(show four p_w_picpath(left, top, right, bottom of text))final TextView textView = (TextView)findViewById(R.id.diet_log_label);final Drawable iconDrawable = getResources().getDrawable(R.drawable.icon);textView.setCompoundDrawablesWithIntrinsicBounds(iconDrawable, iconDrawable, iconDrawable, iconDrawable);// ortextView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon, R.drawable.icon, R.drawable.icon, R.drawable.icon);To show only left p_w_picpath, write "setCompoundDrawablesWithIntrinsicBounds(iconDrawable, null, null, null)"
相關文章
相關標籤/搜索