单个textview中的两种不同样式具有不同的重力和高度 [英] Two different styles in a single textview with different gravity and height

查看:89
本文介绍了单个textview中的两种不同样式具有不同的重力和高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个类似于的文本视图.我已经在下面的代码中实现了此功能.

I am looking for a textview which looks like . I have used below code for the implementation of this..

SpannableString text;
    text = new SpannableString(bal_dollars.getText());
    int index = bal_dollars.getText().toString().indexOf(".");
    text.setSpan(new TextAppearanceSpan(getApplicationContext(),
            R.style.HeroGraphicBalanceSmallFont), index, bal_dollars
            .getText().toString().length(),
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    bal_dollars.setText(text, TextView.BufferType.SPANNABLE);

这是跨度中使用的样式.

here is the style used in the span..

<style name="HeroGraphicBalanceSmallFont" parent="@android:style/TextAppearance.Medium">
    <item name="android:layout_width">wrap_content</item>
    <item name="android:textSize">50sp</item>
    <item name="android:singleLine">true</item>
    <item name="android:layout_height">match_parent</item>
    <item name="android:layout_gravity">top|right</item>
    <item name="android:paddingBottom">30dp</item>
    <item name="android:gravity">top|right</item>
    <item name="android:textColor">@color/status_text</item>
    <item name="customFont">fonts/HeroicCondensedMedium.ttf</item>
</style>

除了文本的目标跨度(即小数部分重力)的间距外,其他一切看起来都很好. .我尝试了所有的xml属性.请帮助

everything looks fine except the spacing of targeted span of text i.e decimal part gravity.. . I tried all the xml attributes. Please help

推荐答案

我破解了它.我发布此消息是因为它可能对其他人有帮助,并感谢@kcoppock的提示. 我扩展了MetricAffectingSpan类,该类以改变字符宽度或高度的方式影响字符级文本格式,从而扩展了该类.

I cracked it. I am posting this as it might be helpful for others and thanks to @kcoppock for the hint. I have extended MetricAffectingSpan class which affects character-level text formatting in a way that changes the width or height of characters extend this class.

public class CustomCharacterSpan extends MetricAffectingSpan {
double ratio = 0.5;

public CustomCharacterSpan() {
}

public CustomCharacterSpan(double ratio) {
    this.ratio = ratio;
}

@Override
public void updateDrawState(TextPaint paint) {
    paint.baselineShift += (int) (paint.ascent() * ratio);
}

@Override
public void updateMeasureState(TextPaint paint) {
    paint.baselineShift += (int) (paint.ascent() * ratio);
}}

使用它来更新视图中的文本.

use this to update the text in your views..

String string = tv.getText().toString();
    SpannableString text = new SpannableString(tv.getText());
    int index = tv.getText().toString().indexOf(".");
    text.setSpan(new CustomCharacterSpan(), index, string.length(),
            SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);//u can use ur own ratio using another constructor
    tv.setText(text, TextView.BufferType.SPANNABLE);

这篇关于单个textview中的两种不同样式具有不同的重力和高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆