同一TextView中字符串的不同字体大小 [英] Different font size of strings in the same TextView

查看:343
本文介绍了同一TextView中字符串的不同字体大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我内部有一个textView,里面有一个数字(变量)和一个string,如何给数字赋予比string大一号的数字? 代码:

I have a textView inside with a number (variable) and a string, how can I give the number one size larger than the string? the code:

TextView size = (TextView)convertView.findViewById(R.id.privarea_list_size);
if (ls.numProducts != null) {
    size.setText(ls.numProducts + " " + mContext.getString(R.string.products));
}

我希望ls.numproducts的大小与文本的其余部分不同.怎么办?

I want ls.numproducts has a size different from the rest of the text. How to do?

推荐答案

使用Spannable String

 String s= "Hello Everyone";
 SpannableString ss1=  new SpannableString(s);
 ss1.setSpan(new RelativeSizeSpan(2f), 0,5, 0); // set size
 ss1.setSpan(new ForegroundColorSpan(Color.RED), 0, 5, 0);// set color
 TextView tv= (TextView) findViewById(R.id.textview);
 tv.setText(ss1); 

快照

您可以使用空格分割字符串,并将跨度添加到所需的字符串中.

You can split string using space and add span to the string you require.

 String s= "Hello Everyone";  
 String[] each = s.split(" ");

现在将span应用于string并将其添加到textview.

Now apply span to the string and add the same to textview.

这篇关于同一TextView中字符串的不同字体大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆