SpannableString: Is it possible to apply two or more RelativeSizeSpans?

跟風遠走 提交于 2019-11-28 03:40:01
Norman

If you're still looking for an answer, I might have a solution. I had similar problems. I used TextUtils to concat the 2 SpannableString.

Here is some example code:

SpannableString span1 = new SpannableString("32m");
SpannableString span2 = new SpannableString("50s");

span1.setSpan(new RelativeSizeSpan(0.75f),  2, 3, 0);
span2.setSpan(new RelativeSizeSpan(0.75f),  2, 3, 0);

mTextView.setText(TextUtils.concat(span1," " ,span2));

You don't need 2 separate SpannableString. A shorter solution can be :

SpannableString span1 = new SpannableString("32m50s");

span1.setSpan(new RelativeSizeSpan(0.75f),  2, 3, 0);
span1.setSpan(new RelativeSizeSpan(0.75f),  5, 6, 0);

mTextView.setText(span1);
AlexPad

You can use array of SpannableString.

Like in this case:

TextView lblDescription=(TextView ) findViewById(R.id.lblDescription);

SpannableString[] finalString = new SpannableString[stringSplit.length];

lblDescription.setText(TextUtils.concat(finalString));

I have using dynamic version of gbero's answer in my project,

public void updateTime(TextView tv) {

    Calendar calendar = Calendar.getInstance();

    SpannableString span = new SpannableString(
            calendar.get(Calendar.HOUR_OF_DAY) + "h" + calendar.get(Calendar.MINUTE) + "m");

    span.setSpan(new RelativeSizeSpan(0.75f), 2, 3, 0);
    span.setSpan(new RelativeSizeSpan(0.75f), 5, 6, 0);

    tv.setText(span);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!