如何在一个文本视图中为两个字符串保留两种不同的字体大小

时间:2014-10-29 10:00:07

标签: java android spannablestring

我有一个自定义类如下:

public class CustomTypefaceSpan extends TypefaceSpan {
    private final Typeface newType;

    public CustomTypefaceSpan(String family, Typeface type) {
        super(family);
        newType = type;
    }

    @Override
    public void updateDrawState(TextPaint ds) {
        applyCustomTypeFace(ds, newType);
    }

    @Override
    public void updateMeasureState(TextPaint paint) {
        applyCustomTypeFace(paint, newType);
    }

    private static void applyCustomTypeFace(Paint paint, Typeface tf) {
        int oldStyle;
        Typeface old = paint.getTypeface();
        if (old == null) {
            oldStyle = 0;
        } else {
            oldStyle = old.getStyle();
        }

        int fake = oldStyle & ~tf.getStyle();
        if ((fake & Typeface.BOLD) != 0) {
            paint.setFakeBoldText(true);
        }

        if ((fake & Typeface.ITALIC) != 0) {
            paint.setTextSkewX(-0.25f);
        }

        paint.setTypeface(tf);
    }
}

我按如下方式应用范围:

Spannable spannable = new SpannableString(dispStatsPart1()+"\n"+dispStatswords()+"Months"); 
                        spannable.setSpan( new CustomTypefaceSpan("sans-serif",Helv_cond_bold), 0, dispStatsPart1().length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                        spannable.setSpan( new CustomTypefaceSpan("sans-serif",helv_light), dispStatsPart1().length(), dispStatsPart1().length() +(dispStatswords()+"Months").length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                        holder.numberText.setText(spannable);

我希望第一行的字体大小不同,第二行的字体大小不同,如何实现?

到目前为止,我已尝试将代码修改为:

private static void applyCustomTypeFace(Paint paint, Typeface tf) {
        int oldStyle;
        Typeface old = paint.getTypeface();
        if (old == null) {
            oldStyle = 0;
        } else {
            oldStyle = old.getStyle();
        }

        int fake = oldStyle & ~tf.getStyle();
        if ((fake & Typeface.BOLD) != 0) {
            paint.setFakeBoldText(true);
        }

        if ((fake & Typeface.ITALIC) != 0) {
            paint.setTextSkewX(-0.25f);
        }
        if(flag == 0){
            paint.setTextSize(50);
        }else{
            paint.setTextSize(50); 
        }

        paint.setTypeface(tf);
    }

我将标志传递给构造函数:

public CustomTypefaceSpan(String family, Typeface type, int flg) {
        super(family);
        newType = type;
        flag = flg; 
    }

但上述似乎不起作用,字体大小增加或减少但两个字符串都会发生。

2 个答案:

答案 0 :(得分:2)

如果您想知道如何在同一文本视图中设置多个不同的大小,但使用绝对大小而不是相对大小,则可以使用AbsoluteSpan实现此目的。

dimens.xml

<dimen name="text_size_1">18sp</dimen>
<dimen name="text_size_2">12sp</dimen>

只需获得所需文字大小的像素尺寸

int textSize1 = getResources().getDimensionPixelSize(R.dimen.text_size_1);
int textSize2 = getResources().getDimensionPixelSize(R.dimen.text_size_2);

然后根据文本

创建一个新的AbsoluteSpan
String text1 = "text1 font size is 18sp";
String text2 = "text2 font size is 12sp ";

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 all
CharSequence finalText = TextUtils.concat(span1, "\n", span2);

这是输出:

Two different font sizes for two strings in one single text view

答案 1 :(得分:0)

yourTextView.setText(Html.fromHtml("xxx"))