Android TextView's subscript being clipped off

前端 未结 9 2380
粉色の甜心
粉色の甜心 2020-12-28 14:41

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

9条回答
  •  情话喂你
    2020-12-28 15:17

    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.

提交回复
热议问题