Textview并不支持所有的html标签。如果更复杂的,可以直接使用webview组件。

AndroidTextView支持的HTML标签

There is a lovely method on theandroid.text.Htmlclass,fromHtml(), that converts HTML into aSpannablefor use with aTextView.

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 filedan issuerequesting 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的意思

一、在xml文件中使用android:textStyle=”bold” 二、但是不能将中文设置成粗体,将中文设置成粗体的方法是:

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 image 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 image next to text.
(show four image(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);
// or
textView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon, R.drawable.icon, R.drawable.icon, R.drawable.icon);

更多相关文章

  1. mybatisplus的坑 insert标签insert into select无参数问题的解决
  2. 2011.06.03(2)——— android 1.6 launcher研究之自定义ViewGroup
  3. Android(安卓)自定义滑动解锁View
  4. Android简易实战教程--第十七话《自定义彩色环形进度条》
  5. Android(安卓)给ImageView自定义设置边框
  6. android解析xml文件的方式
  7. Android自定义属性时TypedArray的使用方法
  8. AndroidManifest 中original-package标签
  9. android 常用控件一览(从底层分析,为自定义控件做下小铺垫)

随机推荐

  1. 【Android】Async异步任务之添加进度条
  2. 在android浏览器中如果安装了app,则打开ap
  3. Qt For Android(安卓)如何获取手机屏幕大
  4. Android系统编译过程中常见问题汇总
  5. 〖Linux〗Android(安卓)NDK调用已编译好
  6. 【Android】 Eclipse : ERROR: Unknown o
  7. [置顶] Cocos2d-x使用过程中遇到的错误(V
  8. 挂断电话的实现(即类似于电话号码黑名单)
  9. Android上的内存监测
  10. [置顶] android加载大图片到内存