简体   繁体   中英

How to resize separate items in TextView Android

My TextView is composed of Tile and Rating with different sizes applied on them. so far I have tried to make i tried to display it using fromHtml But I am facing an issue ie the second part of string that is rating moves to next line all i want both of them to be at same line or better to say rating text followed by title on same line. code:

private void populateTitleView(TextView textView,String title,String rating){
    String ratingText = "";
    if(!rating.equals("")){
        try{
            rating = YooTextUtils.roundOffDecimal(rating,2);
        }catch (Exception ex){

        }

        ratingText = "<span size='12px'>"+rating+" &#9733;</span>";
    }
    String formatedTitle = "<span size='30px'><h1>"+title+"</h1>"+ratingText+"</span>";
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        textView.setText(Html.fromHtml(formatedTitle, Html.FROM_HTML_MODE_COMPACT));
    } else {
        textView.setText(Html.fromHtml(formatedTitle));
    }
}

Desired Result: 所需的图像

use this :

textView.setMaxLines(1);

or in xml textView tag :

android:maxLines="1"

You can use SpannableStringBuilder and RelativeSizeSpan:

String title="";
String rating= "";
String text = title+" "+rating;

SpannableStringBuilder ssBuilder = new SpannableStringBuilder(text);
RelativeSizeSpan bigSize = new RelativeSizeSpan(8.0f);  // try changing these values
RelativeSizeSpan midSize = new RelativeSizeSpan(2.0f);

ssBuilder.setSpan(
            bigSize, // set big size span 
            text.indexOf(title), 
            text.indexOf(title) + String.valueOf(title).length(), 
            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE 
    );
    ssBuilder.setSpan(
            midSize,   // set mid size span
            text.indexOf(rating), 
            text.indexOf(rating) + String.valueOf(rating).length(), 
            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE 
    );

    textView.setText(ssBuilder);  // display the spannable to textview

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