Subscript and Superscript a String in Android

后端 未结 15 1436
面向向阳花
面向向阳花 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:44

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

    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

    0 讨论(0)
  • 2020-11-22 09:48
    ((TextView)findViewById(R.id.text)).setText(Html.fromHtml("X<sup>2</sup>"));
    

    or

    Common Tasks and How to Do Them in Android

    0 讨论(0)
  • 2020-11-22 09:49
    yourTextView.setText(Html.fromHtml("X<sup>2</sup>"));
    
    This will be the result in you yourTextView =
    

    X2

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

    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 ₐ ₑ ₕ ᵢ ⱼ ₖ ₗ ₘ ₙ ₒ ₚ ᵣ ₛ ₜ ᵤ ᵥ ₓ

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

    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 -> ""
    }
    

    }

    0 讨论(0)
提交回复
热议问题