Subscript and Superscript a String in Android

后端 未结 15 1437
面向向阳花
面向向阳花 2020-11-22 09:20

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.

相关标签:
15条回答
  • 2020-11-22 09:54

    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

    0 讨论(0)
  • 2020-11-22 09:55

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

    0 讨论(0)
  • 2020-11-22 09:56

    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>"
    
    0 讨论(0)
  • 2020-11-22 09:56

    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.

    0 讨论(0)
  • 2020-11-22 09:59

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

    textView.setText("X\u00B2");

    0 讨论(0)
  • 2020-11-22 09:59

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

    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)));
    
    0 讨论(0)
提交回复
热议问题