【问题标题】:Subscript and Superscript a String in AndroidAndroid中的下标和上标字符串
【发布时间】:2011-04-02 09:20:23
【问题描述】:

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

【问题讨论】:

标签: java android string superscript subscript


【解决方案1】:
((TextView)findViewById(R.id.text)).setText(Html.fromHtml("X<sup>2</sup>"));

Common Tasks and How to Do Them in Android

【讨论】:

  • 从技术上讲,它不支持 HTML,即创建一个 Spanned,TextViews 支持。本质上是带有样式信息的 CharSequences。
  • 这对我不起作用......但也许是因为我将它设置在我的 strings.xml 文件中。它为我下标,但它会剪辑它,无论我放多少填充,它总是被剪辑。
  • 解析这个 html 是不是非常昂贵?
  • 答案中的链接似乎不再相关。
  • 谢谢你,但下面使用 SpannableStringBuilder 的答案要好得多。
【解决方案2】:

例子:

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

【讨论】:

  • 这个看起来不错,谢谢分享! Html.fromHTML() 方法很方便,但上标不小。
  • 这样更好! Html.fromHtml 方法会导致上标文本被截断。
  • 当我使用这种方法时,比如equation.setText(blah+cs); 说 100 平方米,它不起作用。虽然单独工作正常。如何完成这项工作?
  • 远胜于Html.fromHtml()方法
  • 请补充说明
【解决方案3】:

对于所有询问的人,如果你想让它更小,除了制作上标或下标,你也只需要添加标签。例如:

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

【讨论】:

    【解决方案4】:

    在代码中只要把这个“\u00B2”像这样:

    textView.setText("X\u00B2");

    【讨论】:

    • 您好,您的回答很有帮助。但是,什么代码可以用于下标?如果我用谷歌搜索它们,这些代码又叫什么,它们是 unicode 吗?
    • 嗨,是的,是 unicode,这里是所有 unicode 的 PDF unicode.org/charts/PDF/U2070.pdf
    • 谢谢。很简单。
    【解决方案5】:

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

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

    【讨论】:

      【解决方案6】:

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

           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" 放在您的跨度文本中。

      【讨论】:

        【解决方案7】:
        ((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>
        

        【讨论】:

        • 我是stack over flow的新手,当时不知道怎么用。
        【解决方案8】:

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

        字符串资源:

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

        如果您希望上标更小:

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

        代码:

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

        【讨论】:

          【解决方案9】:

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

          如果您想要代码也考虑到 pre-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

          【讨论】:

            【解决方案10】:

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

            如果您想要的任何字母在这里表示,您实际上不必使用 html 文档

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

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

            例子:

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

            结果:商标ᵀᴹ

            上标和下标字母

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

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

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

            【讨论】:

              【解决方案11】:

              我发现this 文章介绍了如何使用Spannable 或在字符串资源文件中:&lt;sup&gt;&lt;sub&gt; 分别用于上标和下标。

              【讨论】:

              • 答案中的链接似乎不再相关。
              【解决方案12】:

              根据 Gerardo 的回答 here 我在 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 -> ""
              }
              

              }

              【讨论】:

                【解决方案13】:

                strings.xml 文件中,您可以只使用HTML &lt;sup&gt;3&lt;/sup&gt; 标记。为我完美工作

                示例

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

                【讨论】:

                  【解决方案14】:

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

                  【讨论】:

                    【解决方案15】:
                    yourTextView.setText(Html.fromHtml("X<sup>2</sup>"));
                    
                    This will be the result in you yourTextView =
                    

                    X2

                    【讨论】:

                      猜你喜欢
                      • 1970-01-01
                      • 1970-01-01
                      • 2015-05-10
                      • 2013-04-15
                      • 2013-03-26
                      • 2016-01-03
                      • 2012-05-17
                      相关资源
                      最近更新 更多