Android 中字符串的下标和上标
如何打印带有下标或上标的字符串?您可以在没有外部库的情况下做到这一点吗?我希望它显示在 Android 的 TextView
中。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
在代码中只需输入“\u00B2”,如下所示:
textView.setText(“X\u00B2”);
In the code just put this "\u00B2" Like this:
textView.setText("X\u00B2");
它有点晚了,但以下工作正常,使用上标作为特殊字符,我在这里使用了空格字符。
It bit late but following just work fine, use superscript as special character, I used spacial char here.
(或)来自字符串资源文件:
(or) From String Resource File:
已接受的答案现已弃用。所以请仔细阅读这段代码。我从某个网站得到这个。我忘记了名字,但无论如何感谢这段很好的工作代码。
注意:始终放置您的跨文本的
android:textAllCaps="false"
。The Accepted answer is deprecated now. So please go through this piece of code. I got this from some website. I forgot the name but anyway thanks for this good piece of working code.
Note : Always put
android:textAllCaps="false"
of your spantext.如果您想从 string.xml 文件设置上标,请尝试以下操作:
字符串资源:
如果您希望上标更小:
代码:
If you want to set the superscript from string.xml file try this:
string resource:
if you want the superscript to be smaller:
Code:
Android 字符串资源字母的上标和下标
如果此处表示您想要的任何字母,则实际上不必使用 html 文档
对于“a”,复制并粘贴此“ᵃ”
您可以复制并粘贴这些上标和下标中的任何一个直接进入您的 Android 字符串资源。
示例:
结果:商标 ᵀᴹ
上标和下标字母
上标大写 ᴬ ᴮ ᴰ ᴱ ᴳ ᴴ ᴵ ᴶ ᴷ ᴸ ᴹ ᴺ ᴼ ᴾ ᴿ ᵀ ᵁ ⱽ ᵂ
小写字母 ᵃ ᵇ ᶜ ᵈ ᵉ ᶠ ᵍ ʰ ⁱ ʲ ᵏ ˡ ᵐ ⁿ ᵒ ᵖ ʳ ˢ ᵗ ᵘ ᵛ ʷ ˣ ʸ ᶻ
下标小写 ₐ ₑ ₕ ᵢ ⱼ ₖ ₗ ₘ ₙ ₒ ₚ ᵣ ₛ ₜ ᵤ ᵥ ₓ
Android String Resource Superscript and Subscript for letters
You don't really have to use html document if any of the letters you want is represented here
For "a" copy and paste this "ᵃ"
You can copy and paste any of these Superscripts and Subscripts directly into your Android String Resource.
Example:
Result:Trademark ᵀᴹ
Superscript and Subscript letters
Superscript capital ᴬ ᴮ ᴰ ᴱ ᴳ ᴴ ᴵ ᴶ ᴷ ᴸ ᴹ ᴺ ᴼ ᴾ ᴿ ᵀ ᵁ ⱽ ᵂ
Superscript minuscule ᵃ ᵇ ᶜ ᵈ ᵉ ᶠ ᵍ ʰ ⁱ ʲ ᵏ ˡ ᵐ ⁿ ᵒ ᵖ ʳ ˢ ᵗ ᵘ ᵛ ʷ ˣ ʸ ᶻ
Subscript minuscule ₐ ₑ ₕ ᵢ ⱼ ₖ ₗ ₘ ₙ ₒ ₚ ᵣ ₛ ₜ ᵤ ᵥ ₓ
HTML.fromHTML(String) 从 API 24 开始已被弃用。他们说改用这个,它支持标志作为参数。因此,要脱离已接受的答案:
如果您想要也考虑 24 之前的 API 的代码:
该答案源自:
https://stackoverflow.com/a/37905107/4998704
可以在此处找到标志和其他文档:
https://developer.android.com/reference/android/text/Html。 html
The HTML.fromHTML(String) was deprecated as of API 24. They say to use this one instead, which supports flags as a parameter. So to go off of the accepted answer:
And if you want code that considers pre-24 API's as well:
This answer was derived from:
https://stackoverflow.com/a/37905107/4998704
The flags and other documentation can be found here:
https://developer.android.com/reference/android/text/Html.html
我发现这篇文章介绍了如何使用
Spannable< /code> 或在字符串资源文件中:
或
分别表示上标和下标。
I found this article on how to use a
Spannable
or in a string resource file:<sup>
or<sub>
for superscript and subscript, respectively.根据 Gerardo 的回答此处我在 Int 上创建了此扩展
}
Based on Gerardo's answer here I created this extension on Int
}
在
strings.xml
文件中,您可以仅使用 HTML3
标记。非常适合我示例
In the
strings.xml
files, you can just use the HTML<sup>3</sup>
tag. Work perfectly for meEXAMPLE
它们称为 Unicode 字符,Android
TextView
支持它们。从此 Wiki 复制您想要的上标/下标: https://en.wikipedia.org/ wiki/Unicode_characters#Superscripts_and_SubscriptsThey 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_SubscriptsX2
X2
或
常见任务以及如何在 Android 中执行这些任务
or
Common Tasks and How to Do Them in Android
例子:
Example:
对于所有问的人,如果你想让它变得更小,除了制作上标或下标之外,你只需要添加标签即可。前任:
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: