【问题标题】:TextView Superscript not showing correctlyTextView 上标显示不正确
【发布时间】:2018-09-18 15:04:18
【问题描述】:

我在第一个答案中使用找到的代码:Subscript and Superscript a String in Android,但将它与前一个字符串连接起来,所以我的代码类似于:

TextView text = (TextView) findViewById(R.id.text);
text.setText(text.getText().toString()+Html.fromHtml("<sup>-2</sup>"));

假设文本内容为“3x”,使用setText设置文本后,格式化为“3x-2”,不带下标。

TextView 的 XML 是:

<TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="55dp"
            android:layout_marginTop="210dp"
            android:text="3x"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textSize="14pt"
            android:visibility="VISIBLE" 
/>

感谢您的帮助。

【问题讨论】:

    标签: android textview superscript


    【解决方案1】:

    而不是这一行..

    text.setText(text.getText().toString()+Html.fromHtml("<sup>-2</sup>"));
    

    这样试试

     text.setText(Html.fromHtml(text.getText().toString()+"<sup>-2</sup>"));
    

    【讨论】:

    • 谢谢,它成功了。当我再次检查 Stackoverflow 是否有回复时,同样的想法突然出现在我的脑海中。
    • 我可以这样写吗:text.setText("-2"+Html.fromHtml(text.getText().toString()));
    • 我想要这样的 textview *textviewText 而 textview 文本是动态设置的,我想把 * 首先作为 sup 脚本,然后是 textview 文本
    【解决方案2】:

    请检查下面它对我的工作正常

    "TEXT<sup><small>TM</small></sup> TEXT"
    

    上面的代码会使 TM 变为下标,同时也会使文本变小

    【讨论】:

    • 是的,标签 工作正常!非常感谢您的回答!
    【解决方案3】:

    也可以使用小尺寸 -

    text.setText(Html.fromHtml(text.getText().toString()+"<sup><small>-2</small></sup>"));  
    

    将您的整个文本只放入一个Html.fromHtml("Your whole string")

    来自strings.xml

    <string name="superscript_str">YourString&lt;sup&gt;&lt;small&gt;SuperScriptString&lt;/small&gt;&lt;/sup&gt;</string>
    

    【讨论】:

      【解决方案4】:

      如果你想显示小作为超级文本使用

      text.setText(Html.fromHtml(text.getText().toString()+"<sup>small>-2</small>/sup>"));
      

      【讨论】:

        猜你喜欢
        • 2021-10-20
        • 2014-07-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多