两种不同的风格与不同的重力和hieght单一的TextView [英] Two different styles in a single textview with different gravity and hieght

查看:185
本文介绍了两种不同的风格与不同的重力和hieght单一的TextView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要寻找一个TextView它看起来像。我用低于code对本实施..

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);

这篇关于两种不同的风格与不同的重力和hieght单一的TextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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