android textview 支持html标签,Android TextView 支持的HTML标签

强调标签 em 是emphasis的缩写

定义斜体文字

...

段落标签,里面可以加入文字,列表,表格等

定义小字体的文字

定义删除线样式的文字 不符合标准网页设计的理念,不赞成使用. strike是strikethrough的缩写

重点强调标签

下标标签 sub 是subscript的缩写

上标标签 sup 是superscript的缩写

定义monospaced字体的文字 不赞成使用. 此标签对中文没意义 tt是teletype or monospaced text style的意思

定义带有下划线的文字 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:

id="@+id/styled_welcome_message">We are so glad to see you.

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);

To show only left image, write "setCompoundDrawablesWithIntrinsicBounds(iconDrawable, null, null, null)"

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值