gpt4 book ai didi

android - 具有不同重力和高度的单个 TextView 中的两种不同样式

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:00:05 30 4
gpt4 key购买 nike

我正在寻找一个看起来像 enter image description here 的 TextView .我已经使用下面的代码来实现这个..

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>

一切看起来都很好,除了目标文本跨度的间距,即小数部分引力.. enter image description here .我尝试了所有的 xml 属性。请帮忙

最佳答案

我破解了。我发布这个是因为它可能对其他人有帮助,感谢@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);
}}

使用它来更新您 View 中的文本..

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

关于android - 具有不同重力和高度的单个 TextView 中的两种不同样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15356162/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com