2013-03-12 16 views
8

enter image description hereのようなテキストビューを探しています。私は異なる重力と高さを持つ1つのテキストビュー内の2つの異なるスタイル

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

すべてが小数部、すなわち、テキストの目標スパンの間隔を除いて正常に見える...ここ

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

スパンで使用するスタイルです。これを実現するためのコードの下に使用しています重力。enter image description here。私はすべてのXML属性を試しました。助けてください

+2

私はTextPaint' 'の' baselineShift'属性になります。 – kcoppock

+1

@kcoppockに感謝します。私は 'MetricAffectingSpan'クラスを拡張し、' baselineshift'属性を変更しました。それは魅力のように働いた:) – CodeFury

+2

1.5年遅すぎるが[SuperscriptSpan](http://developer.android.com/reference/android/text/style/SuperscriptSpan.html)これのために働く? – andyandy

答えて

11

私はそれをクラックしました。私はこれを他の人に役立つかもしれないので、ヒントのために@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); 
関連する問題