android TextView 设置部分文字背景色和文字颜色
来源:互联网 发布:php php_self 编辑:程序博客网 时间:2024/06/11 20:12
转载于: http://www.2cto.com/kf/201409/335648.html
通过SpannableStringBuilder来实现,它就像html里边的元素改变指定文字的文字颜色或背景色
publicclass MainActivity extendsActivity { @Override protectedvoid onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); String str="这是设置TextView部分文字背景颜色和前景颜色的demo!"; intbstart=str.indexOf("背景"); intbend=bstart+"背景".length(); intfstart=str.indexOf("前景"); intfend=fstart+"前景".length(); SpannableStringBuilder style=newSpannableStringBuilder(str); style.setSpan(newBackgroundColorSpan(Color.RED),bstart,bend,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); style.setSpan(newForegroundColorSpan(Color.RED),fstart,fend,Spannable.SPAN_EXCLUSIVE_INCLUSIVE); TextView tvColor=(TextView) findViewById(R.id.tv_color); tvColor.setText(style); } @Override publicboolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); returntrue; } }
- AbsoluteSizeSpan(int size) ---- 设置字体大小,参数是绝对数值,相当于Word中的字体大小RelativeSizeSpan(float proportion) ---- 设置字体大小,参数是相对于默认字体大小的倍数,比如默认字体大小是x, 那么设置后的字体大小就是x*proportion,这个用起来比较灵活,proportion>1就是放大(zoom in), proportion<1就是缩小(zoom out)
ScaleXSpan(float proportion) ---- 缩放字体,与上面的类似,默认为1,设置后就是原来的乘以proportion,大于1时放大(zoon in),小于时缩小(zoom out)BackgroundColorSpan(int color) ----背景着色,参数是颜色数值,可以直接使用android.graphics.Color里面定义的常量,或是用Color.rgb(int, int, int)ForegroundColorSpan(int color) ----前景着色,也就是字的着色,参数与背景着色一致TypefaceSpan(String family) ----字体,参数是字体的名字比如“sans", "sans-serif"等StyleSpan(Typeface style) -----字体风格,比如粗体,斜体,参数是android.graphics.Typeface里面定义的常量,如Typeface.BOLD,Typeface.ITALIC等等。StrikethroughSpan----如果设置了此风格,会有一条线从中间穿过所有的字,就像被划掉一样
0 0
- android TextView 设置部分文字背景色和文字颜色
- android TextView 设置部分文字背景色和文字颜色
- android TextView 设置部分文字背景色和文字颜色
- Android TextView 设置部分文字背景色和文字颜色
- android TextView 设置部分文字背景色和文字颜色
- TextView设置部分文字背景色和文字颜色
- android 中TextView设置部分文字背景色和文字颜色
- Android TextView 设置部分文字背景色和文字颜色(转)
- Android 动态设置控件文字颜色和背景色
- 如何设置richedit的默认背景色和文字颜色
- 永久设置SecureCRT的背景色和文字颜色方案
- DevGridControl单元格背景色和单元格文字颜色设置
- shell打印菱形和设置文字颜色背景色
- SecureCRT中设置背景色和文字颜色
- Android 关于TextView中,设置指定部分文字改变颜色,和指定部分文字点击事件
- Android学习笔记(三) —— 更改TextView文字颜色及文字背景色
- Android 更改TextView文字颜色(引用Drawable颜色常数及背景色)
- SpannableStringBuilder设置TextView文字格式(颜色、背景色、下划线、删除线等)
- Fragment管理的工具类
- 最大自序和问题
- 为什么基类中的析构函数要声明为虚析构函数?
- 小白的柳州麻将黑科技.Part1 = 你不知道的门清
- iOS图片拉伸技巧
- android TextView 设置部分文字背景色和文字颜色
- CENTOS 基本安装-切换到root用户;非图形界面启动;设置静态IP;PSCP文件传输
- java笔记25 File类、递归、Properties
- SCU 4440
- UVa 200 - Rare Order
- 1、Kafka伪分布安装
- hdoj1723_Distribute Message(dp)
- 面向对象(三)
- Spark编程指引(四)------------------共享变量(广播变量和累加器)