不同大小的一个TextView的文本设置 [英] Set text of a textview with different sizes

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

问题描述

我有TextView的,我想在文本中的每个字是用不同的大小和字体。

I have TextView and I want each word of the text to be with different size and font.

我知道,你可以设置文本的一部分,要大胆或与和标签下划线。

I know that you can set part of the text to be bold or with underline with the and tags.

但如果我需要的文字部分是18sp的大小,并有一个自定义的字体(从资产),而另一部分有不同的自定义字体和大小14sp的。

But what if I need part of the text to be the size of 18sp and have a custom typeface(from assets) and another part to have a different custom typeface and a size of 14sp.

我可以分割的TextView个人TextViews,但我想避免这种情况。

推荐答案

您应该使用 RelativeSizeSpan ,供参考<一个href=\"http://developer.android.com/reference/android/text/style/RelativeSizeSpan.html\">RelativeSizeSpan针对不同的字体大小。

You should use RelativeSizeSpan ,for Reference RelativeSizeSpan for different font sizes.

TextView tv;
final SpannableString text = new SpannableString("Hello World");
text.setSpan(new RelativeSizeSpan(2.0f), 0, 5,
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
text.setSpan(new RelativeSizeSpan(1.5f), 5, 11,
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

tv.setText(text);

有关fontstyle的使用 StyleSpan 参考 StyleSpan

For FontStyle use StyleSpan for reference StyleSpan

所以使用可以使用自定义的字体的帮助下 StyleSpan

So use can use custom typeface with the help of StyleSpan.

text.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC), start,
                    end, Spannable.SPAN_INCLUSIVE_INCLUSIVE);

希望这会帮助你。

Hope this will help you.

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

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