简体   繁体   English

如何在TextView Android中调整单独项目的大小

[英]How to resize separate items in TextView Android

My TextView is composed of Tile and Rating with different sizes applied on them. 我的TextView由图块和评级组成,并在其上应用了不同的大小。 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. 到目前为止,我试图使我尝试使用fromHtml来显示它,但是我遇到了一个问题,即评级的字符串的第二部分移至下一行,我希望它们都位于同一行或更好地表示评级文本然后在同一行上加上标题。 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 : 或在xml textView标记中:

android:maxLines="1"

You can use SpannableStringBuilder and RelativeSizeSpan: 您可以使用SpannableStringBuilder和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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Android中将TextView分为2个单独的TextView - How to divide TextView into 2 Separate TextView in Android 如何防止TextView调整父级LinearLayout的大小-Android - How to prevent a TextView resize the parent LinearLayout - Android 如何根据Android的分辨率调整TextView的大小 - How to put a TextView resize depending on the resolution of Android 如何在Android Studio中的TextView中自动调整文本大小? - how to auto resize text in TextView in Android Studio? 如何使用SeekBar的进度调整TextView的大小(Android) - How to resize TextView with SeekBar's progress (Android) 如何在 Android 的 ListView Items 中动态添加 TextView? - How to add A TextView Dynamically in the ListView Items Android? 如何在 android 中的 TextView 上设置 Recyclview 项目 - How to set Recyclview items on TextView in android 如何将 TextView 放在 Android 中的 RecyclerView 项目上? - How to put a TextView over RecyclerView items in Android? 在 TextView android 中调整可绘制图像的大小 - Resize drawable image in TextView android 在 Android 中,给定带有 TextView 和 RadioGroup 的项目的 ListView,如何动态添加项目? - In Android, given a ListView of items with TextView and RadioGroup, how to add items dynamically?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM