简体   繁体   English

Android TextView 的下标被剪掉

[英]Android TextView's subscript being clipped off

The Android TextView clips off my text subscripts (see image below) even when I use android:layout_height="wrap_content" for the TextView.即使我对 TextView 使用android:layout_height="wrap_content" ,Android TextView 也会剪掉我的文本下标(见下图)。 Is there a fix/work-around for this?是否有解决方案/解决方法?

替代文字

P/S: Superscripts work fine P/S:上标工作正常

Note: padding doesn't work.注意:填充不起作用。

  • I tried even adding a padding of 50dip but it did not help.我什至尝试添加50dip的填充,但没有帮助。
  • I can use an absolute height such as 50dip but that messes everything up when I need text to wrap around.我可以使用绝对高度,例如50dip,但是当我需要文本环绕时,这会把一切搞砸

Sample Code:示例代码:

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

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.理想情况下,我们希望 Android 在计算行高时考虑下标/上标。 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.诀窍在于 BufferType.SPANNABLE。 Apparently it makes TextView pay more attention to the markup and calculate line heights properly.显然它使 TextView 更加关注标记并正确计算行高。

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>"));

android:lineSpacingExtra="4dp" should solve it this will add extra line spacing below your text, and keep subscript from getting cutoff. android:lineSpacingExtra="4dp" 应该解决这个问题,这将在文本下方添加额外的行距,并防止下标被截断。 I haven't tried it with superscript so it might now fix that.我还没有用上标尝试过,所以它现在可能会解决这个问题。

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>));

I had the same issue, so after reading the posts, I found this to be working.我有同样的问题,所以在阅读帖子后,我发现这有效。

Example : H 2 O示例:H 2 O
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. BufferType.SPANNABLE 很重要,因为它会告诉 textview 考虑上标跨度。

If you are using custom tag handler for HTML you can also use it like this:如果您为 HTML 使用自定义标签处理程序,您也可以像这样使用它:

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

Hope it helps someone looking for same problem.希望它可以帮助寻找同样问题的人。

I have faced the same issue in ICS and below android versions.我在 ICS 及以下 android 版本中遇到了同样的问题。 I fixed the issue by a simple step我通过一个简单的步骤解决了这个问题

Give a minimum height to the Text View .给 Text View 一个最小高度。 It will fix the problem.它将解决问题。

You can set minimum height through xml .您可以通过 xml 设置最小高度。

        android:minHeight="30dp"

Or dynamically或动态

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

            tv.setMinHeight(52);
    }

This worked for me along with the Small tag.这与Small标签一起对我有用。

  1. Inside the TextView add在 TextView 添加

android:paddingBottom="1dp"机器人:paddingBottom =“1dp”

  1. Use the small Tag after the subscript下标后使用小Tag

yourTextView.setText(Html.fromHtml("" +" Hey< sub >< small >2< /small > < /sub >")); yourTextView.setText(Html.fromHtml("" +" 嘿< 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.注意请注意,第 1 步很重要,我的文字在某些情况下仍然被削减,使用 paddingBottom 解决了它。 Don't forget to remove the spaces in sub and small tags that are present in my answer :)不要忘记删除我的答案中存在的子标签和小标签中的空格:)

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. Html.fromHtml 对我不起作用,它要么剪掉了顶部,要么剪掉了底部。

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.我最终使用带有 SubscriptSpan 或 SuperscriptSpan 的 SpannableString,然后在 TextAppearanceSpan 中设置字体大小。

Once I had done that I had to expand the height of the TextView as well.完成后,我还必须扩展 TextView 的高度。

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. CalcSize 返回显示元素数组中最大字符串的边界矩形。

fractionAdjustment is an emperically selected value that works for the selected font size adjusted for screen geometry. fractionAdjustment 是一个凭经验选择的值,适用于为屏幕几何形状调整的选定字体大小。

Note: This is TextView is inside a ListView, so that might have some impact as well.注意:这是在 ListView 中的 TextView,因此也可能会产生一些影响。

// 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());

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. <small> </small> 标签的数量越多,下标就越小,您应该能够看到它而不会被剪裁。

Eg: H 2 O例如:H 2 O

Html.fromHtml("H<sub><small><small><small>2</small></small></small></sub>O");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM