同一TextView中字符串的不同字体大小

我的textView内部有一个数字(变量)和a string,我怎样才能给数字大一个大于的大小string?编码:


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的大小与文本的其余部分不同。怎么做?


蝴蝶刀刀
浏览 613回答 3
3回答

牧羊人nacy

万一您想知道如何在同一个文本视图中设置多个不同的大小,但是使用绝对大小而不是相对大小,则可以使用AbsoluteSizeSpan而不是来实现RelativeSizeSpan。只需获得所需文本大小的尺寸(以像素为单位)int textSize1 = getResources().getDimensionPixelSize(R.dimen.text_size_1);int textSize2 = getResources().getDimensionPixelSize(R.dimen.text_size_2);然后AbsoluteSpan根据文本创建一个新的String text1 = "Hi";String text2 = "there";SpannableString span1 = new SpannableString(text1);span1.setSpan(new AbsoluteSizeSpan(textSize1), 0, text1.length(), SPAN_INCLUSIVE_INCLUSIVE);SpannableString span2 = new SpannableString(text2);span2.setSpan(new AbsoluteSizeSpan(textSize2), 0, text2.length(), SPAN_INCLUSIVE_INCLUSIVE);// let's put both spans together with a separator and allCharSequence finalText = TextUtils.concat(span1, " ", span2);

大话西游666

方法1public static void increaseFontSizeForPath(Spannable spannable, String path, float increaseTime) {    int startIndexOfPath = spannable.toString().indexOf(path);    spannable.setSpan(new RelativeSizeSpan(increaseTime), startIndexOfPath,            startIndexOfPath + path.length(), 0);}使用Utils.increaseFontSizeForPath(spannable, "big", 3); // make "big" text bigger 3 time than normal text方法二public static void setFontSizeForPath(Spannable spannable, String path, int fontSizeInPixel) {    int startIndexOfPath = spannable.toString().indexOf(path);    spannable.setSpan(new AbsoluteSizeSpan(fontSizeInPixel), startIndexOfPath,            startIndexOfPath + path.length(), 0);}使用Utils.setFontSizeForPath(spannable, "big", (int) textView.getTextSize() + 20); // make "big" text bigger 20px than normal text
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android