Android中更改字体颜色的多种方法
2017-04-13 17:12
330 查看
在开发中,偶尔要单独更改TextView中某些字的字体颜色,慢慢摸索出几种方法,之后有发现会再更新...
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----如果设置了此风格,会有一条线从中间穿过所有的字,就像被划掉一样
原文:http://www.2cto.com/kf/201409/335648.html
其实就是单独设置了font块的字体颜色,是Html的语法,在setText()中调用Html.fromHtml方法进行解析显示出来。
若有发现其他方法会再继续更新~~~
1.通过SpannableStringBuilder来实现,它就像html里边的元素改变指定文字的文字颜色或背景色
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); String str="这是设置TextView部分文字背景颜色和前景颜色的demo!"; int bstart=str.indexOf("背景"); int bend=bstart+"背景".length(); int fstart=str.indexOf("前景"); int fend=fstart+"前景".length(); SpannableStringBuilder style=new SpannableStringBuilder(str); style.setSpan(new BackgroundColorSpan(Color.RED),bstart,bend,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); style.setSpan(new ForegroundColorSpan(Color.RED),fstart,fend,Spannable.SPAN_EXCLUSIVE_INCLUSIVE); TextView tvColor=(TextView) findViewById(R.id.tv_color); tvColor.setText(style); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
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----如果设置了此风格,会有一条线从中间穿过所有的字,就像被划掉一样
原文:http://www.2cto.com/kf/201409/335648.html
2.通过解析HTML代码的方法实现
textView = (TextView) findViewById(R.id.textView); String string = " 我已阅读并同意 "; textView.setText(Html.fromHtml(string));
其实就是单独设置了font块的字体颜色,是Html的语法,在setText()中调用Html.fromHtml方法进行解析显示出来。
若有发现其他方法会再继续更新~~~
相关文章推荐
- android 更改TextView中任意位置字体大小和颜色的方法
- android TextView的字体颜色设置的多种方法(转)
- android TextView的字体颜色设置的多种方法
- android TextView的字体颜色设置的多种方法(续)
- android TextView的字体颜色设置的多种方法
- android TextView的字体颜色设置的多种方法
- android TextView的字体颜色设置的多种方法
- Android设置TextView字体呈现多种颜色的方法
- 【Android基础】TextView的字体颜色设置的多种方法
- android 设置字体颜色、EditText自己主动输入转换成大写字母的多种方式
- TextView的字体颜色设置的多种方法
- android改变字体的颜色的三种方法
- Android 修改spinner 字体颜色 样式的方法
- android radiobutton选中字体颜色改变的方法
- android 设置字体颜色、EditText自动输入转换成大写字母的多种方式
- Android AlertDialog更改标题颜色,字体等
- Android 通过按钮Button更改全部的TextView、EditText、Button的字体大小、字体颜色、背景颜色
- Android 改变字体颜色的三种方法
- Android开发笔记 改变字体颜色的三种方法
- Android一个TextView设置多种颜色的2种高效方法