Android TextView's subscript being clipped off

2020-02-20 07:30发布

The Android TextView clips off my text subscripts (see image below) even when I use android:layout_height="wrap_content" for the TextView. Is there a fix/work-around for this?

alt text

P/S: Superscripts work fine

Note: padding doesn't work.

  • I tried even adding a padding of 50dip but it did not help.
  • I can use an absolute height such as 50dip but that messes everything up when I need text to wrap around.

Sample Code:

mtTextView.setText(Html.fromHtml("HC0<sub>3</sub>"));

9条回答
Juvenile、少年°
2楼-- · 2020-02-20 07:49

I had the same issue, so after reading the posts, I found this to be working.

Example : H2O
simply use :

textView.setText(Html.fromHtml("H<sub>2</sub>O"),BufferType.SPANNABLE);

BufferType.SPANNABLE is important as it will tell textview to consider the superscript span.

If you are using custom tag handler for HTML you can also use it like this:

 textView.setText(Html.fromHtml(data, null, new CustomHtmlTagHandler(),BufferType.SPANNABLE);

Hope it helps someone looking for same problem.

查看更多
一夜七次
3楼-- · 2020-02-20 07:52

I have faced the same issue in ICS and below android versions. I fixed the issue by a simple step

Give a minimum height to the Text View . It will fix the problem.

You can set minimum height through xml .

        android:minHeight="30dp"

Or dynamically

 if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN) {

            tv.setMinHeight(52);
    }
查看更多
\"骚年 ilove
4楼-- · 2020-02-20 07:58

This worked for me along with the Small tag.

  1. Inside the TextView add

android:paddingBottom="1dp"

  1. Use the small Tag after the subscript

yourTextView.setText(Html.fromHtml("" +" Hey< sub >< small >2< /small > < /sub >"));

Note Please note , step 1 is important , My text was still cutting down in some case,using paddingBottom resolved it. Don't forget to remove the spaces in sub and small tags that are present in my answer :)

查看更多
仙女界的扛把子
5楼-- · 2020-02-20 08:04

For subscript a slight variation to the above suggestion is needed, two small tags:

    textView.setText(Html.fromHtml(
        "HCO<sub><small><small>3</small></small></sub>));
查看更多
爱情/是我丢掉的垃圾
6楼-- · 2020-02-20 08:04

The More number of <small> </small> tags in there, the smaller the subscript will get and you should be able to see it without being clipped.

Eg: H2O

Html.fromHtml("H<sub><small><small><small>2</small></small></small></sub>O");
查看更多
老娘就宠你
7楼-- · 2020-02-20 08:08

Most answers suggest to add paddings or to use smaller sub/superscripts. These might be serviceable workarounds, but they don't really solve the problem. Ideally, we want Android to take the sub/superscript into account when calculating line height. I think I found how to do it, and I'm sharing it for people googling this issue.

    SpannableStringBuilder sb = new SpannableStringBuilder("X2");
    sb.setSpan(new SuperscriptSpan(), 1, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    textView.setText(sb, BufferType.SPANNABLE);

The trick is in BufferType.SPANNABLE. Apparently it makes TextView pay more attention to the markup and calculate line heights properly.

查看更多
登录 后发表回答