Android TextView的下标被剪掉

发布于 2024-08-11 00:17:36 字数 524 浏览 7 评论 0原文

即使我为 TextView 使用 android:layout_height="wrap_content" ,Android TextView 也会剪掉我的文本下标(见下图)。 有解决办法/解决方法吗?

替代文本

P/S:上标工作正常

注意:填充不起作用。

  • 我什至尝试添加 50dip 的填充,但没有帮助。
  • 我可以使用绝对高度,例如 50dip,但是当我需要文本环绕时,这会弄乱一切。

示例代码:

mtTextView.setText(Html.fromHtml("HC03"));

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

—━☆沉默づ 2024-08-18 00:17:36

大多数答案建议添加填充或使用较小的子/上标。这些可能是有用的解决方法,但它们并不能真正解决问题。理想情况下,我们希望 Android 在计算行高时考虑下标/上标。我想我找到了如何做到这一点,并且我将其分享给那些在谷歌上搜索此问题的人。

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

诀窍在于 BufferType.SPANNABLE。显然,它使 TextView 更加关注标记并正确计算行高。

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.

好听的两个字的网名 2024-08-18 00:17:36

这个解决方案对我有用。

当浏览器渲染时,上标文本通常会变小,这似乎不会发生在这里,因此您可以通过执行以下操作来复制它(并解决此问题):

someTextView.setText(Html.fromHtml("Some text<sup><small>1</small></sup>"));

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>"));
凉栀 2024-08-18 00:17:36

对于下标,需要对上述建议稍作修改,两个小标签:

    textView.setText(Html.fromHtml(
        "HCO<sub><small><small>3</small></small></sub>));

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>));
殊姿 2024-08-18 00:17:36

android:lineSpacingExtra="4dp" 应该可以解决它
这将在文本下方添加额外的行距,并防止下标被截断。我还没有尝试过使用上标,所以现在可能会解决这个问题。

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.

老旧海报 2024-08-18 00:17:36

我也遇到了同样的问题,所以在阅读了帖子后,我发现这是有效的。

示例:H2O
只需使用:

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

BufferType.SPANNABLE 很重要,因为它会告诉 textview 考虑上标范围。

如果您正在使用 HTML 的自定义标记处理程序,您也可以像这样使用它:

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

希望它可以帮助寻找相同问题的人。

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.

往事随风而去 2024-08-18 00:17:36

我显示分数和带分数,因此我同时使用超级和下标。 Html.fromHtml 对我不起作用,它要么剪掉了顶部,要么剪掉了底部。

奇怪的是,混合数可以正确工作,但分数本身却不能。

我最终使用了带有 SubscriptSpan 或 SuperscriptSpan 的 SpannableString,然后在 TextAppearanceSpan 中设置字体大小。

完成后,我还必须扩展 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);

然后我必须扩展高度:

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

CalcSize 返回显示元素数组中最大字符串的边界矩形。

fractionAdjustment 是一个经验选择的值,适用于根据屏幕几何形状调整的选定字体大小。

注意:这是 TextView 位于 ListView 内部,因此这也可能会产生一些影响。

// 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;
}

经验值:

fractionAdjustment = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 14, resources.getDisplayMetrics());
fractionFontSize = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 11, resources.getDisplayMetrics());

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());
我一向站在原地 2024-08-18 00:17:36

我在 ICS 及以下 Android 版本中遇到了同样的问题。我通过一个简单的步骤解决了这个问题

为 Text View 设置最小高度。它会解决这个问题。

您可以通过 xml 设置最小高度。

        android:minHeight="30dp"

或者动态地

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

            tv.setMinHeight(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);
    }
御弟哥哥 2024-08-18 00:17:36

这与 Small 标签一起对我有用。

  1. 在 TextView 里面添加

android:paddingBottom="1dp"

  1. 使用下标后的小标签

yourTextView.setText(Html.fromHtml("" +" 嘿<小>2"));

注意请注意,第1步很重要,我的文本在某些情况下仍然被削减,使用paddingBottom解决了它。
不要忘记删除我的答案中存在的子标签和小标签中的空格:)

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

温柔嚣张 2024-08-18 00:17:36

<小>的数量越多, 标签在那里,下标会变得越小,你应该能够看到它而不会被剪切。

例如:H2O

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

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");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文