代码之家  ›  专栏  ›  技术社区  ›  Tawani

android textview的下标被剪掉了

  •  20
  • Tawani  · 技术社区  · 15 年前

    android textview即使在我使用时也会剪掉我的文本下标(见下图)。 android:layout_height="wrap_content" 对于文本视图。 有没有解决办法?

    alt text http://i38.tinypic.com/qy8n7m.png

    P/S:上标很好用

    注: 填充不起作用。

    • 我甚至试着加衬垫 50DIP 但没有帮助。
    • 我可以使用绝对高度,例如 50度 但当我需要文本环绕时,这会把一切都搞砸。

    样例代码:

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

    9 回复  |  直到 8 年前
        1
  •  19
  •   Tristan Warner-Smith    13 年前

    This solution 为我工作。

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

    someTextView.setText(Html.fromHtml("Some text<sup><small>1</small></sup>"));
    
        2
  •  13
  •   Nick Frolov    9 年前

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

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

    技巧在bufferType.spannable中。显然,它使textview更加关注标记并正确计算行高。

        3
  •  10
  •   Dan Robby Pond    13 年前

    对于下标,需要对上述建议进行细微的更改,需要两个小标签:

        textView.setText(Html.fromHtml(
            "HCO<sub><small><small>3</small></small></sub>));
    
        4
  •  10
  •   PetaroMitaro    11 年前

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

        5
  •  5
  •   Priya Singhal    8 年前

    我也有同样的问题,所以在阅读了这些帖子后,我发现这是可行的。

    例子:H o
    简单使用:

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

    buffertype.spanable很重要,因为它会告诉textview考虑上标span。

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

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

    希望它能帮助人们寻找同样的问题。

        6
  •  1
  •   grumble    12 年前

    我显示分数和混合数,所以我同时使用了super和subscripting。html.fromhtml对我来说不起作用,它要么夹在顶部,要么夹在底部。

    奇怪的是,混合数工作正常,但分数本身没有。

    最后我使用了一个带下标或上标的spannableString,然后在文本外观span中设置字体大小。

    一旦我做到了这一点,我还必须扩展文本视图的高度。

    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是一个皇帝选择的值,适用于为屏幕几何调整的选定字体大小。

    注意:这是一个位于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;
    }
    

    经验值:

    fractionAdjustment = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 14, resources.getDisplayMetrics());
    fractionFontSize = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 11, resources.getDisplayMetrics());
    
        7
  •  1
  •   Muhamed Riyas M    11 年前

    在ICS和Android版本下,我也面临同样的问题。我简单地解决了这个问题

    为文本视图提供最小高度。它可以解决问题。

    可以通过XML设置最小高度。

            android:minHeight="30dp"
    

    或动态

     if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN) {
    
                tv.setMinHeight(52);
        }
    
        8
  •  1
  •   Ajji    8 年前

    这对我和 小的 标签。

    1. 在文本视图中添加

    安卓:paddingbottom=“1dp”

    1. 在下标后使用小标记

    yourtextview.settext(html.fromhtml(“”+“嘿<sub><small>2</small></sub>“);

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

        9
  •  0
  •   Ram Iyer    10 年前

    其中的<small></small>标记的数量越多,下标将越小,您应该能够在不被剪裁的情况下看到它。

    Eg:H o

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