8

のようなテキストビューを探していますここに画像の説明を入力してください。これを実装するために、以下のコードを使用しました。

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>

テキストのターゲットスパンの間隔、つまり小数部分の重力を除いて、すべてが正常に見えますここに画像の説明を入力してください。すべてのxml属性を試しました。助けてください

4

1 に答える 1

13

私はそれを割った。他の人に役立つ可能性があるため、これを投稿しています。ヒントを提供してくれた @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);
于 2013-03-12T10:40:46.880 に答える