如何在android textview中显示分数?

时间:2014-04-02 07:01:00

标签: android textview

我想在android文本视图上显示。我尝试用unicode做但没有运气。请参阅下面的代码。任何人都可以帮忙。

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

5 个答案:

答案 0 :(得分: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

答案 1 :(得分: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。 :以上代码未经过测试(只是预感)

答案 2 :(得分:1)

你可以直接使用unicode字符作为

tv.setText("3\u00BD");

这对我有用。

答案 3 :(得分:0)

试试这个:

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

答案 4 :(得分:0)

我建议您在TextView中创建两个RelativeLayout并进行管理。因为在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>