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
Subscript and Superscript a String in Android
提问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 TextView
in Android.
如何打印带有下标或上标的字符串?你能在没有外部库的情况下做到这一点吗?我希望它显示TextView
在 Android 中。
采纳答案by Konstantin Burov
((TextView)findViewById(R.id.text)).setText(Html.fromHtml("X<sup>2</sup>"));
or
或者
回答by Mohit Deshpande
回答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<sup>3</sup></string>
if you want the superscript to be smaller:
如果您希望上标更小:
<string name="test_string">X<sup><small>3</small></sup></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.xml
files, 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 TextView
supports 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