单个文本视图中具有不同重力和高度的两种不同样式

2023-11-21

I am looking for a textview which looks like enter image description here. 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);

这是跨度中使用的样式..

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

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


我破解了它。我发布此内容是因为它可能对其他人有帮助,并感谢@kcoppock 的提示。 我已经延长了MetricAffectingSpan通过更改字符宽度或高度的方式影响字符级文本格式的类扩展了此类。

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

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

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);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

单个文本视图中具有不同重力和高度的两种不同样式 的相关文章

随机推荐