2014-04-02 113 views
6

我想在android文本視圖中顯示。我試着用unicode做,但沒有運氣。請參閱下面的代碼。任何人都可以請幫忙。如何在android textview中顯示分數?

additionalInfoString = additionalInfoString.replace("½",<sup><small>&frac12;</small></sup>); 
+0

看看這個希望它的幫助http://stackoverflow.com/questions/2920726/android-displaying-fractions-using-unicode –

回答

7

您需要使用HTML格式來顯示分數

tv.setText(Html.fromHtml("3<sup>1</sup>/<sub>2</sub>")); 

驗證你的HTML文本here

enter image description here

作爲Html.fromHtml(String s)方法已折舊。看看這個答案SO Answer

+0

感謝üSlientkiller ......它的工作...但我的問題不是它在textview上被截斷。你可以爲我設置任何東西 – androidDeveloer

+0

@androidDeveloer你需要爲此設置固定高度。 – SilentKiller

+0

謝謝你SlientKiller ....現在工作正常:))) – androidDeveloer

1

您可以使用Android TextView的html格式。但是,您必須在頂部和底部添加一個額外的空間以防止餾分被切斷。

SpannableStringBuilder text = new SpannableStringBuilder(); 
    text.append("3"); 
    text.append("\n"); 
    text.append(Html.fromHtml("<sup>1</sup>/<sub>2</sub>")); 
    text.append("\n"); 

P.S. :上面的代碼沒有進行測試(只是一種預感)

0

試試這個:

mTxtVw.setText(Html.fromHtml("3<sup>1</sup>/<sub>2</sub>")); 
1

可以使用Unicode字符直接作爲

tv.setText("3\u00BD"); 

這爲我工作。

0

我建議你在RelativeLayout中創建兩個TextView並對其進行管理。因爲Html.fromHtml上標文字沒有自帶對齊。使用一些東西像下面

<RelativeLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" > 

    <TextView 
     android:id="@+id/maintext_sup" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:gravity="top" 
     android:text="1/2" 
     android:textSize="10sp"/> 

    <TextView 
     android:id="@+id/maintext" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     android:gravity="center" 
     android:text="3" 
     android:textSize="20sp" /> 
    </RelativeLayout>