[英]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+" ★</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));
}
}
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.