Java Android 中的字符串下标和上标

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3543454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 01:43:27  来源:igfitidea点击:

Subscript and Superscript a String in Android

javaandroidstringsuperscriptsubscript

提问by Mohit Deshpande

How can you print a string with a subscript or superscript? Can you do this without an external library? I want this to display in a TextViewin Android.

如何打印带有下标或上标的字符串?你能在没有外部库的情况下做到这一点吗?我希望它显示TextView在 Android 中。

采纳答案by Konstantin Burov

((TextView)findViewById(R.id.text)).setText(Html.fromHtml("X<sup>2</sup>"));

or

或者

Common Tasks and How to Do Them in Android

常见任务以及如何在 Android 中完成它们

回答by Mohit Deshpande

I found thisarticle on how to use a Spannableor in a string resource file: <sup>or <sub>for superscript and subscript, respectively.

我发现这个文章对如何使用Spannable或在一个字符串资源文件:<sup><sub>分别标和下标。

回答by peter

Example:

例子:

equation = (TextView) findViewById(R.id.textView1);
SpannableStringBuilder cs = new SpannableStringBuilder("X3 + X2");
cs.setSpan(new SuperscriptSpan(), 1, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
cs.setSpan(new RelativeSizeSpan(0.75f), 1, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
cs.setSpan(new SuperscriptSpan(), 6, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
cs.setSpan(new RelativeSizeSpan(0.75f), 6, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
equation.setText(cs);

回答by Ariel Capozzoli

To all people asking, if you want to make it smaller besides of making super or subscript, you just need to add tag as well. EX:

对于所有询问的人,如果除了制作上标或下标之外,还想让它变小,您只需要添加标签即可。前任:

"X <sup><small> 2 </small></sup>"

回答by César Cobo

If you want to set the superscript from string.xml file try this:

如果你想从 string.xml 文件设置上标,试试这个:

string resource:

字符串资源:

<string name="test_string">X&lt;sup&gt;3&lt;/sup&gt;</string>

if you want the superscript to be smaller:

如果您希望上标更小:

<string name="test_string">X&lt;sup&gt;&lt;small&gt;3&lt;/small&gt;&lt;/sup&gt;</string>

Code:

代码:

textView.setText(Html.fromHtml("Anything you want to put here"+getString(R.string.test_string)));

回答by m.v.n.kalyani

((TextView)findViewById(R.id.text)).setText(Html.fromHtml("X<sup><small>2</small></sup>")); 

(or) From String Resource File:

(或)从字符串资源文件:

<string name="test_string">
  <![CDATA[ X<sup><small>2</small></sup> ]]>
</string>

回答by CoDe

It bit late but following just work fine, use superscript as special character, I used spacial char here.

有点晚了,但以下工作正常,使用上标作为特殊字符,我在这里使用了空格字符。

<string name="str">H?</string>

回答by Detilium

In the strings.xmlfiles, you can just use the HTML <sup>3</sup>tag. Work perfectly for me

strings.xml文件中,您可以只使用 HTML<sup>3</sup>标记。非常适合我

EXAMPLE

例子

<string name="turnoverRate">Turnover rate m<sup>3</sup>/m<sup>2</sup>/hour:</string>

回答by Aditya Naique

They are called Unicode characters, and Android TextViewsupports them. Copy the super/sub-script you want from this Wiki: https://en.wikipedia.org/wiki/List_of_Unicode_characters#Superscripts_and_Subscripts

它们被称为 Unicode 字符,AndroidTextView支持它们。从这个 Wiki 复制你想要的超级/子脚本:https: //en.wikipedia.org/wiki/List_of_Unicode_characters#Superscripts_and_Subscripts

回答by ンドリュー ライアンア

yourTextView.setText(Html.fromHtml("X<sup>2</sup>"));

This will be the result in you yourTextView =

X2

× 2