简体   繁体   中英

How to change textsize of a word in a string Android?

I have a String which should shows my value out of a total value, Like 2/5 ,But what i want to do is show it as 2 or the number before the "/" is of a comparatively larger size then the number after the "/" . This is what i have tried so far, and it has no effect.

    SpannableString ss1=  new SpannableString(String.valueOf(connectedDevices + "/" + totalDevices));
    ss1.setSpan(new RelativeSizeSpan(2f), 0,1, 0); // set size
    mData.get(0).setCurrentCount(String.valueOf(ss1) );
    notifyDataSetChanged();

I want the size of connectedDevices to be N (eg N=3) times of the size of totalDevices. How can I format this string?

The difference from the link this question is marked as duplicate is that I am storing this text in a string or in any variable and then using it later on through the adapter of a recyclerview.

A plain String does not hold any span formatting information, so the String.valueOf(ss1) call is basically undoing the formatting you've done with the SpannableString and the RelativeSizeSpan .

To preserve that formatting, change the type of the relevant field, as well as the parameter types on any corresponding methods, to CharSequence , which is the base interface that pretty much all textual data types implement. You would then pass that SpannableString directly to setCurrentCount() , instead of first converting it to a String .

If, at some point, you do need a flat String , you can convert it then; eg, with toString() or String.valueOf() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM