简体   繁体   中英

display fraction in android

I want to display fraction in android. But it is displayed as 1/2. I don't want this. I want exactly how described here in this question.

how is it possible to get it in TextView?

Why don't you use a WebView and the html code as described in your linked question?

http://developer.android.com/reference/android/webkit/WebView.html

For a limited subset, you could also use the following unicode characters in a TextView: ¼½¾

chm2.setText(Html.fromHtml("<sup>12</sup>&frasl;<sub>6</sub>"));

or

chm5.setText(Html.fromHtml("<u>1</u><br/>" +
            "&emsp;2"));

here

&emsp;

is used for giving space infront of second line for compatible position

I hardly think so . A TextView shows text. That's almost everytime done using specific representation languages (like HTML, but for math), and thus your input would only show the representation language (with the tags and so) if it is unable to understand it, and not the resulting formula.

Depending on your requirements, you could also achieve this by using 3 TextViews and a Shape to draw the line (a rectangle):

|---------- |      [topTextView]
|           |
|bigTextView| ---[rectangle shape]---
|           |
|---------- |    [bottomTextView]

You can create a layout as follows

<RelativeLayout
    android:id="@+id/RelativeLayout"
    android:layout_width="40dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginTop="10dp"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textTop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="0"
        android:textColor="@color/gray"
        android:textSize="14sp" />

    <TextView
        android:id="@+id/textSlash"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_gravity="center"
        android:layout_toRightOf="@+id/textTop"
        android:text="/"
        android:textColor="@color/gray" />

    <TextView
        android:id="@+id/textBottom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textSlash"
        android:layout_gravity="right"
        android:layout_toRightOf="@+id/textSlash"
        android:text="10"
        android:textColor="@color/gray"
        android:textSize="14sp" />

 </RelativeLayout>

I'm sure this can be improved on - but gives the basic idea.

在字符串中,您可以在' U \\ 00BC '中使用Unicode,即(1/4)。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM