TextView中的文本加效果,如设置不同颜色,大小或点击事件

方法一

利用HTML语言,改变文字颜色

例1:

 String str = "<font color='red'>中软</font>" + "<font color= 'green'>国际</font>";  
  TextView tv = new TextView(this);  
  tv.setText(Html.fromHtml(str)); 

例2:

<string name="link_text_manual"><b>text2:</b> This is some other   
      text, with a <a href="http://www.google.com">link</a> specified via an <a> tag.  Use a \"tel:\" URL   
      to <a href="tel:4155551212">dial a phone number</a>   
</string>


方法二

通过定义不同的style来设置TextView的颜色和大小

例1:

 <style name="style0">  
    <item name="android:textSize">19dip</item>  
    <item name="android:textColor">@color/color1</item>  
       </style>  
  
<style name="style1">  
    <item name="android:textSize">23dip</item>  
    <item name="android:textColor">@color/color2</item>  
    <item name="android:textStyle">italic</item>  
</style>  

SpannableString styledText = new SpannableString("大家好才是真的好");  
styledText.setSpan(new TextAppearanceSpan(this, R.style.style0), 0, 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  
styledText.setSpan(new TextAppearanceSpan(this, R.style.style1), 3, 5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  
          
mTextView.setText(styledText, TextView.BufferType.SPANNABLE);


方法三

例1:

自动识别链接
在TextView中添加如下属性

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值