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

时间:2021-08-01 20:11:22

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.

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

14 个解决方案

#1


135  

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

or

要么

Common Tasks and How to Do Them in Android

常见任务及其在Android中的应用

#2


92  

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

#3


40  

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:

对于所有人来说,除了制作超级或下标之外,如果你想让它变小,你只需要添加标签。 EX:

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

#4


8  

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

#5


7  

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

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

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

#6


6  

((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>

#7


3  

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

我发现这篇文章关于如何使用Spannable或字符串资源文件:分别用于上标和下标。

#8


3  

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:

从API 24开始,不推荐使用HTML.fromHTML(String)。他们说要使用这个,而是支持flags作为参数。所以要取消接受的答案:

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:

如果您想要考虑前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>"));    
}

This answer was derived from: https://*.com/a/37905107/4998704

这个答案来自:https://*.com/a/37905107/4998704

The flags and other documentation can be found here: https://developer.android.com/reference/android/text/Html.html

标志和其他文档可以在这里找到:https://developer.android.com/reference/android/text/Html.html

#9


3  

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.

注意:总是把你的spantext的android:textAllCaps =“false”。

#10


3  

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

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

textView.setText("X\u00B2");

textView.setText( “X \ u00B2”);

#11


0  

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字符,Android TextView支持它们。从此Wiki复制所需的超级/子脚本:https://en.wikipedia.org/wiki/List_of_Unicode_characters#Superscripts_and_Subscripts

#12


0  

For XML file, just like in strings.xml: <string name="main_step1">1<sup><small>st</small></sup></string> Add tag you want.

对于XML文件,就像在strings.xml中一样: 1 st 添加所需的标记。

#13


-1  

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

在strings.xml文件中,您只需使用HTML 3 标记。对我来说很完美

EXAMPLE

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

#14


-1  

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

This will be the result in you yourTextView =

X2

X2

#1


135  

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

or

要么

Common Tasks and How to Do Them in Android

常见任务及其在Android中的应用

#2


92  

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

#3


40  

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:

对于所有人来说,除了制作超级或下标之外,如果你想让它变小,你只需要添加标签。 EX:

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

#4


8  

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

#5


7  

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

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

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

#6


6  

((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>

#7


3  

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

我发现这篇文章关于如何使用Spannable或字符串资源文件:分别用于上标和下标。

#8


3  

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:

从API 24开始,不推荐使用HTML.fromHTML(String)。他们说要使用这个,而是支持flags作为参数。所以要取消接受的答案:

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:

如果您想要考虑前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>"));    
}

This answer was derived from: https://*.com/a/37905107/4998704

这个答案来自:https://*.com/a/37905107/4998704

The flags and other documentation can be found here: https://developer.android.com/reference/android/text/Html.html

标志和其他文档可以在这里找到:https://developer.android.com/reference/android/text/Html.html

#9


3  

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.

注意:总是把你的spantext的android:textAllCaps =“false”。

#10


3  

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

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

textView.setText("X\u00B2");

textView.setText( “X \ u00B2”);

#11


0  

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字符,Android TextView支持它们。从此Wiki复制所需的超级/子脚本:https://en.wikipedia.org/wiki/List_of_Unicode_characters#Superscripts_and_Subscripts

#12


0  

For XML file, just like in strings.xml: <string name="main_step1">1<sup><small>st</small></sup></string> Add tag you want.

对于XML文件,就像在strings.xml中一样: 1 st 添加所需的标记。

#13


-1  

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

在strings.xml文件中,您只需使用HTML 3 标记。对我来说很完美

EXAMPLE

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

#14


-1  

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

This will be the result in you yourTextView =

X2

X2