关于TextView中修改部分字体颜色、大小方法的总结


TextView属性描述

  1. 属性名称    描述  
  2. android:autoLink    设置是否当文本为URL链接/email/电话号码/map时,文本显示为可点击的链接。可选值(none/web/email/phone/map/all)  
  3. android:autoText    如果设置,将自动执行输入值的拼写纠正。此处无效果,在显示输入法并输入的时候起作用。  
  4. android:bufferType  指定getText()方式取得的文本类别。选项editable 类似于StringBuilder可追加字符,  
  5. 也就是说getText后可调用append方法设置文本内容。spannable 则可在给定的字符区域使用样式,参见这里1、这里2。  
  6.   
  7. android:capitalize  设置英文字母大写类型。此处无效果,需要弹出输入法才能看得到,参见EditView此属性说明。  
  8. android:cursorVisible   设定光标为显示/隐藏,默认显示。  
  9. android:digits  设置允许输入哪些字符。如“1234567890.+-*/%\n()”  
  10. android:drawableBottom  在text的下方输出一个drawable,如图片。如果指定一个颜色的话会把text的背景设为该颜色,并且同时和background使用时覆盖后者。  
  11. android:drawableLeft    在text的左边输出一个drawable,如图片。  
  12. android:drawablePadding 设置text与drawable(图片)的间隔,与drawableLeft、drawableRight、drawableTop、drawableBottom一起使用,可设置为负数,单独使用没有效果。  
  13. android:drawableRight   在text的右边输出一个drawable,如图片。  
  14. android:drawableTop 在text的正上方输出一个drawable,如图片。  
  15. android:editable    设置是否可编辑。这里无效果,参见EditView。  
  16. android:editorExtras    设置文本的额外的输入数据。在EditView再讨论。  
  17. android:ellipsize   设置当文字过长时,该控件该如何显示。有如下值设置:”start”—–省略号显示在开头;”end”——省略号显示在结尾;”middle”—-省略号显示在中间;”marquee” ——以跑马灯的方式显示(动画横向移动)  
  18.   
  19. android:freezesText 设置保存文本的内容以及光标的位置。参见:这里。  
  20.   
  21. android:gravity 设置文本位置,如设置成“center”,文本将居中显示。  
  22. android:hint    Text为空时显示的文字提示信息,可通过textColorHint设置提示信息的颜色。此属性在EditView中使用,但是这里也可以用。  
  23. android:imeOptions  附加功能,设置右下角IME动作与编辑框相关的动作,如actionDone右下角将显示一个“完成”,而不设置默认是一个回车符号。这个在EditView中再详细说明,此处无用。  
  24. android:imeActionId 设置IME动作ID。在EditView再做说明,可以先看这篇帖子:这里。  
  25.   
  26. android:imeActionLabel  设置IME动作标签。在EditView再做说明。  
  27. android:includeFontPadding  设置文本是否包含顶部和底部额外空白,默认为true。  
  28. android:inputMethod 为文本指定输入法,需要完全限定名(完整的包名)。例如:com.google.android.inputmethod.pinyin,但是这里报错找不到。  
  29. android:inputType   设置文本的类型,用于帮助输入法显示合适的键盘类型。在EditView中再详细说明,这里无效果。  
  30. android:marqueeRepeatLimit  在ellipsize指定marquee的情况下,设置重复滚动的次数,当设置为marquee_forever时表示无限次。  
  31. android:ems 设置TextView的宽度为N个字符的宽度。这里测试为一个汉字字符宽度,如图:   
  32. android:maxEms  设置TextView的宽度为最长为N个字符的宽度。与ems同时使用时覆盖ems选项。  
  33. android:minEms  设置TextView的宽度为最短为N个字符的宽度。与ems同时使用时覆盖ems选项。  
  34. android:maxLength   限制显示的文本长度,超出部分不显示。  
  35. android:lines   设置文本的行数,设置两行就显示两行,即使第二行没有数据。  
  36. android:maxLines    设置文本的最大显示行数,与width或者layout_width结合使用,超出部分自动换行,超出行数将不显示。  
  37. android:minLines    设置文本的最小行数,与lines类似。  
  38. android:linksClickable  设置链接是否点击连接,即使设置了autoLink。  
  39. android:lineSpacingExtra    设置行间距。  
  40. android:lineSpacingMultiplier   设置行间距的倍数。如”1.2”  
  41. android:numeric 如果被设置,该TextView有一个数字输入法。此处无用,设置后唯一效果是TextView有点击效果,此属性在EdtiView将详细说明。  
  42. android:password    以小点”.”显示文本  
  43. android:phoneNumber 设置为电话号码的输入方式。  
  44. android:privateImeOptions   设置输入法选项,此处无用,在EditText将进一步讨论。  
  45. android:scrollHorizontally  设置文本超出TextView的宽度的情况下,是否出现横拉条。  
  46. android:selectAllOnFocus    如果文本是可选择的,让他获取焦点而不是将光标移动为文本的开始位置或者末尾位置。TextView中设置后无效果。  
  47. android:shadowColor 指定文本阴影的颜色,需要与shadowRadius一起使用。效果:    
  48. android:shadowDx    设置阴影横向坐标开始位置。  
  49. android:shadowDy    设置阴影纵向坐标开始位置。  
  50. android:shadowRadius    设置阴影的半径。设置为0.1就变成字体的颜色了,一般设置为3.0的效果比较好。  
  51. android:singleLine  设置单行显示。如果和layout_width一起使用,当文本不能全部显示时,后面用“…”来表示。如android:text="test_ singleLine " android:singleLine="true" android:layout_width="20dp"将只显示“t…”。如果不设置singleLine或者设置为false,文本将自动换行  
  52. android:text    设置显示文本.  
  53. android:textAppearance  设置文字外观。如“?android:attr/textAppearanceLargeInverse  
  54. ”这里引用的是系统自带的一个外观,?表示系统是否有这种外观,否则使用默认的外观。可设置的值如下:textAppearanceButton/textAppearanceInverse/textAppearanceLarge/textAppearanceLargeInverse/textAppearanceMedium/textAppearanceMediumInverse/textAppearanceSmall/textAppearanceSmallInverse  
  55. android:textColor   设置文本颜色  
  56. android:textColorHighlight  被选中文字的底色,默认为蓝色  
  57. android:textColorHint   设置提示信息文字的颜色,默认为灰色。与hint一起使用。  
  58. android:textColorLink   文字链接的颜色.  
  59. android:textScaleX  设置文字之间间隔,默认为1.0f。分别设置0.5f/1.0f/1.5f/2.0f效果如下:  
  60.    
  61. android:textSize    设置文字大小,推荐度量单位”sp”,如”15sp”  
  62. android:textStyle   设置字形[bold(粗体) 0, italic(斜体) 1, bolditalic(又粗又斜) 2] 可以设置一个或多个,用“|”隔开  
  63. android:typeface    设置文本字体,必须是以下常量值之一:normal 0, sans 1, serif 2, monospace(等宽字体) 3]   
  64. android:height  设置文本区域的高度,支持度量单位:px(像素)/dp/sp/in/mm(毫米)  
  65. android:maxHeight   设置文本区域的最大高度  
  66. android:minHeight   设置文本区域的最小高度  
  67. android:width   设置文本区域的宽度,支持度量单位:px(像素)/dp/sp/in/mm(毫米),与layout_width的区别看这里。  
  68.   
  69. android:maxWidth    设置文本区域的最大宽度  
  70. android:minWidth    设置文本区域的最小宽度 


 

  1. textView = (TextView) findViewById(R.id.textview);  
  2. SpannableStringBuilder builder = new SpannableStringBuilder(textView.getText().toString());  
  3.   
  4. //ForegroundColorSpan 为文字前景色,BackgroundColorSpan为文字背景色  
  5. ForegroundColorSpan redSpan = new ForegroundColorSpan(Color.RED);  
  6. ForegroundColorSpan whiteSpan = new ForegroundColorSpan(Color.WHITE);  
  7. ForegroundColorSpan blueSpan = new ForegroundColorSpan(Color.BLUE);  
  8. ForegroundColorSpan greenSpan = new ForegroundColorSpan(Color.GREEN);  
  9. ForegroundColorSpan yellowSpan = new ForegroundColorSpan(Color.YELLOW);  
  10.   
  11.   
  12.   
  13. builder.setSpan(redSpan, 01, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
  14. builder.setSpan(whiteSpan, 12, Spannable.SPAN_INCLUSIVE_INCLUSIVE);  
  15. builder.setSpan(blueSpan, 23, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
  16. builder.setSpan(greenSpan, 34, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
  17. builder.setSpan(yellowSpan, 4,5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
  18.   
  19. textView.setText(builder);  

我想把TextView设置成如下图所示的效果,一开始我是想用textView.setText("已为<font size=18sp>88</font>位")类似的代码实现,于是发现Android自带的这样一个方法Html.fromHtml(),即可以实现在文字里加入html标签的样式,但是该方法font标签并没有size这个属性,只有color和face。因此只能通过big和small来控制字体大小,代码如下:textView.setText(Html.fromHtml("今日已为<big>88</big>位")); 即可改变部分字体大小。





Android TextView部分字体变色或字体变大小 

方法详解必看

A. 

SpannableStringBuilder style=new SpannableStringBuilder(str);
//SpannableStringBuilder实现CharSequence接口
style.setSpan(new ForegroundColorSpan(Color.RED), 0, 2,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE );
style.setSpan(new ForegroundColorSpan(Color.YELLOW), 2, 4,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE );
style.setSpan(new ForegroundColorSpan(Color.GREEN), 4, 6,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE );
tv.setText(style);//将其添加到tv中 


String html = "预计收益<font color='#ff0000'>"+et_buy_sum.getText().toString()+"</font>";
tv_profit_think.setText(Html.fromHtml(html));






B


  1. TextView textView1 = (TextView) findViewById(R.id.textView1);  
  2.         TextView textView2 = (TextView) findViewById(R.id.textView2);  
  3.         TextView textView3 = (TextView) findViewById(R.id.textView3);  
  4.         TextView textView4 = (TextView) findViewById(R.id.textView4);  
  5.   
  6.         //两次加大字体,设置字体为红色(big会加大字号,font可以定义颜色)  
  7.         textView1.setText(Html.fromHtml("北京市发布霾黄色预警,<font color='#ff0000'><big><big>外出携带好</big></big></font>口罩"));  
  8.   
  9.         //设置字体大小为3级标题,设置字体为红色  
  10.         textView2.setText(Html.fromHtml("北京市发布霾黄色预警,<h3><font color='#ff0000'>外出携带好</font></h3>口罩"));  
  11.   
  12.         //设置字体大小为58(单位为物理像素),设置字体为红色,字体背景为黄色  
  13.         textView3.setText("北京市发布霾黄色预警,外出携带好口罩");  
  14.         Spannable span = new SpannableString(textView3.getText());  
  15.         span.setSpan(new AbsoluteSizeSpan(58), 1116, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
  16.         span.setSpan(new ForegroundColorSpan(Color.RED), 1116, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
  17.         span.setSpan(new BackgroundColorSpan(Color.YELLOW), 1116, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
  18.         textView3.setText(span);  
  19.   
  20.         //两次缩小字体,设置字体为红色(small可以减小字号)  
  21.         textView4.setText(Html.fromHtml("北京市发布霾黄色预警,<font color='#ff0000'><small><small>外出携带好</small></small></font>口罩")); 


 

  1. textView = (TextView) findViewById(R.id.textview);  
  2. SpannableStringBuilder builder = new SpannableStringBuilder(textView.getText().toString());  
  3.   
  4. //ForegroundColorSpan 为文字前景色,BackgroundColorSpan为文字背景色  
  5. ForegroundColorSpan redSpan = new ForegroundColorSpan(Color.RED);  
  6. ForegroundColorSpan whiteSpan = new ForegroundColorSpan(Color.WHITE);  
  7. ForegroundColorSpan blueSpan = new ForegroundColorSpan(Color.BLUE);  
  8. ForegroundColorSpan greenSpan = new ForegroundColorSpan(Color.GREEN);  
  9. ForegroundColorSpan yellowSpan = new ForegroundColorSpan(Color.YELLOW);  
  10.   
  11.   
  12.   
  13. builder.setSpan(redSpan, 01, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
  14. builder.setSpan(whiteSpan, 12, Spannable.SPAN_INCLUSIVE_INCLUSIVE);  
  15. builder.setSpan(blueSpan, 23, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
  16. builder.setSpan(greenSpan, 34, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
  17. builder.setSpan(yellowSpan, 4,5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
  18.   
  19. textView.setText(builder);  


步骤如下:

      1.定义不同style .

不妨如下定义2个style

 

  1.        <style name="style0">  
  2.     <item name="android:textSize">19dip</item>  
  3.     <item name="android:textColor">@color/color1</item>  
  4.        </style>  
  5.   
  6. <style name="style1">  
  7.     <item name="android:textSize">23dip</item>  
  8.     <item name="android:textColor">@color/color2</item>  
  9.     <item name="android:textStyle">italic</item>  
  10. </style>  

 2 . 通过SpannableString 设置字符串格式。代码如下:

  1. <span style="white-space:pre">        </span>mTextView = (TextView)findViewById(R.id.test);  
  2.           
  3.          SpannableString styledText = new SpannableString("亲爱的小宝,你好");  
  4.          styledText.setSpan(new TextAppearanceSpan(this, R.style.style0), 03, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  
  5.          styledText.setSpan(new TextAppearanceSpan(this, R.style.style1), 35, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  
  6.           
  7.         mTextView.setText(styledText, TextView.BufferType.SPANNABLE);  

实际效果如下:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值