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条回答
放我归山
2楼-- · 2020-02-20 08:08

android:lineSpacingExtra="4dp" should solve it this will add extra line spacing below your text, and keep subscript from getting cutoff. I haven't tried it with superscript so it might now fix that.

查看更多
祖国的老花朵
3楼-- · 2020-02-20 08:10

This solution worked for me.

Superscripted text is usually made smaller when the browser renders it, that doesn't seem to happen here so you can replicate that (and solve this problem) by doing this:

someTextView.setText(Html.fromHtml("Some text<sup><small>1</small></sup>"));
查看更多
Deceive 欺骗
4楼-- · 2020-02-20 08:11

I'm displaying fractions and mixed numbers so I'm using both super and subscripting together. The Html.fromHtml didn't work for me, it either clipped the top or the bottom.

Oddly, mixed numbers worked correctly, but fractions by themselves did not.

I ended up using a SpannableString with a SubscriptSpan or a SuperscriptSpan, then setting the font size in a TextAppearanceSpan.

Once I had done that I had to expand the height of the TextView as well.

TextView number = (TextView)findViewById(R.id.number);
String temp = "1 1/2";
SpannableString s = new SpannableString(temp);
// if the string has a fraction in it, superscript the numerator and subscript the denominator
if (temp.indexOf('/') != -1)
{
    int len = temp.length();
    s.setSpan(new SuperscriptSpan(), len - 3, len - 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    s.setSpan(new TextAppearanceSpan(null, 0, fractionFontSize, null, null), len - 3, len - 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    s.setSpan(new TextAppearanceSpan(null, 0, fractionFontSize, null, null), len - 2, len - 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    s.setSpan(new SubscriptSpan(), len - 1, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    s.setSpan(new TextAppearanceSpan(null, 0, fractionFontSize, null, null), len - 1, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
number.setText(s);

Then I had to expand the height:

RelativeLayout.LayoutParams parms = (RelativeLayout.LayoutParams)number.getLayoutParams();
Rect frame = CalcSize(number.getTextSize(), quantityMaxString);
parms.height = frame.height() + fractionAdjustment;
number.setLayoutParams(parms);

CalcSize returns a bounding rectangle of the largest string in the array of display elements.

fractionAdjustment is an emperically selected value that works for the selected font size adjusted for screen geometry.

Note: This is TextView is inside a ListView, so that might have some impact as well.

// calculate the field dimensions, given the font size and longest string
private static Rect CalcSize(float fontSize, String maxString)
{
    Rect bounds = new Rect();
    paint.setTypeface(Typeface.DEFAULT);
    paint.setTextSize(fontSize);

    paint.getTextBounds(maxString, 0, maxString.length(), bounds);

    return bounds;
}

Empirical values:

fractionAdjustment = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 14, resources.getDisplayMetrics());
fractionFontSize = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 11, resources.getDisplayMetrics());
查看更多
登录 后发表回答