Subscript and Superscript a String in Android

后端 未结 15 1630
面向向阳花
面向向阳花 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("X2", 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("X2", Html.FROM_HTML_MODE_LEGACY));
    } else {
        textView.setText(Html.fromHtml("X2"));    
    }
    

    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

提交回复
热议问题