Android 中字符串的下标和上标

发布于 2024-09-17 23:36:27 字数 85 浏览 5 评论 0原文

如何打印带有下标或上标的字符串?您可以在没有外部库的情况下做到这一点吗?我希望它显示在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(15

2024-09-24 23:36:27
((TextView)findViewById(R.id.text)).setText(Html.fromHtml("X<sup>2</sup>"));

常见任务以及如何在 Android 中执行这些任务

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

or

Common Tasks and How to Do Them in Android

叫思念不要吵 2024-09-24 23:36:27

例子:

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);

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);
银河中√捞星星 2024-09-24 23:36:27

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

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

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>"
木槿暧夏七纪年 2024-09-24 23:36:27

在代码中只需输入“\u00B2”,如下所示:

textView.setText(“X\u00B2”);

In the code just put this "\u00B2" Like this:

textView.setText("X\u00B2");

七分※倦醒 2024-09-24 23:36:27

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

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

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

<string name="str">H₂</string>
无声静候 2024-09-24 23:36:27
((TextView)findViewById(R.id.text)).setText(Html.fromHtml("X<sup><small>2</small></sup>")); 

(或)来自字符串资源文件:

<string name="test_string">
  <![CDATA[ X<sup><small>2</small></sup> ]]>
</string>
((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>
忘年祭陌 2024-09-24 23:36:27

已接受的答案现已弃用。所以请仔细阅读这段代码。我从某个网站得到这个。我忘记了名字,但无论如何感谢这段很好的工作代码。

     SpannableString styledString
            = new SpannableString("Large\n\n"     // index 0 - 5
            + "Bold\n\n"          // index 7 - 11
            + "Underlined\n\n"    // index 13 - 23
            + "Italic\n\n"        // index 25 - 31
            + "Strikethrough\n\n" // index 33 - 46
            + "Colored\n\n"       // index 48 - 55
            + "Highlighted\n\n"   // index 57 - 68
            + "K Superscript\n\n" // "Superscript" index 72 - 83 
            + "K Subscript\n\n"   // "Subscript" index 87 - 96
            + "Url\n\n"           //  index 98 - 101
            + "Clickable\n\n");   // index 103 - 112

    // make the text twice as large
    styledString.setSpan(new RelativeSizeSpan(2f), 0, 5, 0);

    // make text bold
    styledString.setSpan(new StyleSpan(Typeface.BOLD), 7, 11, 0);

    // underline text
    styledString.setSpan(new UnderlineSpan(), 13, 23, 0);

    // make text italic
    styledString.setSpan(new StyleSpan(Typeface.ITALIC), 25, 31, 0);

    styledString.setSpan(new StrikethroughSpan(), 33, 46, 0);

    // change text color
    styledString.setSpan(new ForegroundColorSpan(Color.GREEN), 48, 55, 0);

    // highlight text
    styledString.setSpan(new BackgroundColorSpan(Color.CYAN), 57, 68, 0);

    // superscript
    styledString.setSpan(new SuperscriptSpan(), 72, 83, 0);
    // make the superscript text smaller
    styledString.setSpan(new RelativeSizeSpan(0.5f), 72, 83, 0);

    // subscript
    styledString.setSpan(new SubscriptSpan(), 87, 96, 0);
    // make the subscript text smaller
    styledString.setSpan(new RelativeSizeSpan(0.5f), 87, 96, 0);

    // url
    styledString.setSpan(new URLSpan("http://www.google.com"), 98, 101, 0);

    // clickable text
    ClickableSpan clickableSpan = new ClickableSpan() {

        @Override
        public void onClick(View widget) {
            // We display a Toast. You could do anything you want here.
            Toast.makeText(MainActivity.this, "Clicked", Toast.LENGTH_SHORT).show();

        }
    };

    styledString.setSpan(clickableSpan, 103, 112, 0);


    // Give the styled string to a TextView
    spantext = (TextView) findViewById(R.id.spantext);


    // this step is mandated for the url and clickable styles.
    spantext.setMovementMethod(LinkMovementMethod.getInstance());

    // make it neat
    spantext.setGravity(Gravity.CENTER);
    spantext.setBackgroundColor(Color.WHITE);

    spantext.setText(styledString);

注意:始终放置您的跨文本的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.

     SpannableString styledString
            = new SpannableString("Large\n\n"     // index 0 - 5
            + "Bold\n\n"          // index 7 - 11
            + "Underlined\n\n"    // index 13 - 23
            + "Italic\n\n"        // index 25 - 31
            + "Strikethrough\n\n" // index 33 - 46
            + "Colored\n\n"       // index 48 - 55
            + "Highlighted\n\n"   // index 57 - 68
            + "K Superscript\n\n" // "Superscript" index 72 - 83 
            + "K Subscript\n\n"   // "Subscript" index 87 - 96
            + "Url\n\n"           //  index 98 - 101
            + "Clickable\n\n");   // index 103 - 112

    // make the text twice as large
    styledString.setSpan(new RelativeSizeSpan(2f), 0, 5, 0);

    // make text bold
    styledString.setSpan(new StyleSpan(Typeface.BOLD), 7, 11, 0);

    // underline text
    styledString.setSpan(new UnderlineSpan(), 13, 23, 0);

    // make text italic
    styledString.setSpan(new StyleSpan(Typeface.ITALIC), 25, 31, 0);

    styledString.setSpan(new StrikethroughSpan(), 33, 46, 0);

    // change text color
    styledString.setSpan(new ForegroundColorSpan(Color.GREEN), 48, 55, 0);

    // highlight text
    styledString.setSpan(new BackgroundColorSpan(Color.CYAN), 57, 68, 0);

    // superscript
    styledString.setSpan(new SuperscriptSpan(), 72, 83, 0);
    // make the superscript text smaller
    styledString.setSpan(new RelativeSizeSpan(0.5f), 72, 83, 0);

    // subscript
    styledString.setSpan(new SubscriptSpan(), 87, 96, 0);
    // make the subscript text smaller
    styledString.setSpan(new RelativeSizeSpan(0.5f), 87, 96, 0);

    // url
    styledString.setSpan(new URLSpan("http://www.google.com"), 98, 101, 0);

    // clickable text
    ClickableSpan clickableSpan = new ClickableSpan() {

        @Override
        public void onClick(View widget) {
            // We display a Toast. You could do anything you want here.
            Toast.makeText(MainActivity.this, "Clicked", Toast.LENGTH_SHORT).show();

        }
    };

    styledString.setSpan(clickableSpan, 103, 112, 0);


    // Give the styled string to a TextView
    spantext = (TextView) findViewById(R.id.spantext);


    // this step is mandated for the url and clickable styles.
    spantext.setMovementMethod(LinkMovementMethod.getInstance());

    // make it neat
    spantext.setGravity(Gravity.CENTER);
    spantext.setBackgroundColor(Color.WHITE);

    spantext.setText(styledString);

Note : Always put android:textAllCaps="false" of your spantext.

弱骨蛰伏 2024-09-24 23:36:27

如果您想从 string.xml 文件设置上标,请尝试以下操作:

字符串资源:

<string name="test_string">X<sup>3</sup></string>

如果您希望上标更小:

<string name="test_string">X<sup><small>3</small></sup></string>

代码:

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

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

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)));
┾廆蒐ゝ 2024-09-24 23:36:27

Android 字符串资源字母的上标和下标

如果此处表示您想要的任何字母,则实际上不必使用 html 文档

对于“a”,复制并粘贴此“ᵃ”

您可以复制并粘贴这些上标和下标中的任何一个直接进入您的 Android 字符串资源。

示例:

    <string name="word_with_superscript" translatable="false">Trademark ᵀᴹ</string>

结果:商标 ᵀᴹ

上标和下标字母

上标大写 ᴬ ᴮ ᴰ ᴱ ᴳ ᴴ ᴵ ᴶ ᴷ ᴸ ᴹ ᴺ ᴼ ᴾ ᴿ ᵀ ᵁ ⱽ ᵂ

小写字母 ᵃ ᵇ ᶜ ᵈ ᵉ ᶠ ᵍ ʰ ⁱ ʲ ᵏ ˡ ᵐ ⁿ ᵒ ᵖ ʳ ˢ ᵗ ᵘ ᵛ ʷ ˣ ʸ ᶻ

下标小写 ₐ ₑ ₕ ᵢ ⱼ ₖ ₗ ₘ ₙ ₒ ₚ ᵣ ₛ ₜ ᵤ ᵥ ₓ

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:

    <string name="word_with_superscript" translatable="false">Trademark ᵀᴹ</string>

Result:Trademark ᵀᴹ

Superscript and Subscript letters

Superscript capital ᴬ ᴮ ᴰ ᴱ ᴳ ᴴ ᴵ ᴶ ᴷ ᴸ ᴹ ᴺ ᴼ ᴾ ᴿ ᵀ ᵁ ⱽ ᵂ

Superscript minuscule ᵃ ᵇ ᶜ ᵈ ᵉ ᶠ ᵍ ʰ ⁱ ʲ ᵏ ˡ ᵐ ⁿ ᵒ ᵖ ʳ ˢ ᵗ ᵘ ᵛ ʷ ˣ ʸ ᶻ

Subscript minuscule ₐ ₑ ₕ ᵢ ⱼ ₖ ₗ ₘ ₙ ₒ ₚ ᵣ ₛ ₜ ᵤ ᵥ ₓ

勿忘初心 2024-09-24 23:36:27

HTML.fromHTML(String) 从 API 24 开始已被弃用。他们说改用这个,它支持标志作为参数。因此,要脱离已接受的答案:

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

如果您想要也考虑 24 之前的 API 的代码:

TextView textView = ((TextView)findViewById(R.id.text));
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
  textView.setText(Html.fromHtml("X<sup>2</sup>", Html.FROM_HTML_MODE_LEGACY));
} else {
    textView.setText(Html.fromHtml("X<sup>2</sup>"));    
}

该答案源自:
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:

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

And if you want code that considers pre-24 API's as well:

TextView textView = ((TextView)findViewById(R.id.text));
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
  textView.setText(Html.fromHtml("X<sup>2</sup>", Html.FROM_HTML_MODE_LEGACY));
} else {
    textView.setText(Html.fromHtml("X<sup>2</sup>"));    
}

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

川水往事 2024-09-24 23:36:27

我发现这篇文章介绍了如何使用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.

掌心的温暖 2024-09-24 23:36:27

根据 Gerardo 的回答此处我在 Int 上创建了此扩展

fun Int.toSuperScript(): String {
    return when (this) {
        0 -> "\u2070"
        1 -> "\u00B9"
        2 -> "\u00B2"
        3 -> "\u00B3"
        4 -> "\u2074"
        5 -> "\u2075"
        6 -> "\u2076"
        7 -> "\u2077"
        8 -> "\u2078"
        9 -> "\u2079"
        else -> ""
}

}

Based on Gerardo's answer here I created this extension on Int

fun Int.toSuperScript(): String {
    return when (this) {
        0 -> "\u2070"
        1 -> "\u00B9"
        2 -> "\u00B2"
        3 -> "\u00B3"
        4 -> "\u2074"
        5 -> "\u2075"
        6 -> "\u2076"
        7 -> "\u2077"
        8 -> "\u2078"
        9 -> "\u2079"
        else -> ""
}

}

原野 2024-09-24 23:36:27

strings.xml 文件中,您可以仅使用 HTML 3 标记。非常适合我

示例

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

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

EXAMPLE

<string name="turnoverRate">Turnover rate m<sup>3</sup>/m<sup>2</sup>/hour:</string>
故人的歌 2024-09-24 23:36:27

它们称为 Unicode 字符,Android TextView 支持它们。从此 Wiki 复制您想要的上标/下标: https://en.wikipedia.org/ wiki/List_of_Unicode_characters#Superscripts_and_Subscripts

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

少女的英雄梦 2024-09-24 23:36:27
yourTextView.setText(Html.fromHtml("X<sup>2</sup>"));

This will be the result in you yourTextView =

X2

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

This will be the result in you yourTextView =

X2

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文