textview cutting off a letter in android

2020-06-30 05:20发布

http://dl.dropbox.com/u/24856/Screenshots/android/cutoff.png

this is a screen shot from my android. the text is "asd". however the "d" is slightly cut off. here is the relevant view:

        <TextView
            android:id="@+id/stuff"
            android:padding="2dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/other_stuff"
            android:layout_marginTop="33dp"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textStyle="italic" />

any idea as to what is causing this ?

标签: android
11条回答
可以哭但决不认输i
2楼-- · 2020-06-30 05:41

could be the padding, try setting that to 0dp

also is there anything else in the layout?

查看更多
女痞
3楼-- · 2020-06-30 05:43

To narrow down the source of the error try setting android:letterSpacing="0" This was the reason for cut off letters in my case. Another hint for too much letter spacing is that the cut off part get bigger when the text has more letters in it.

查看更多
再贱就再见
4楼-- · 2020-06-30 05:44

This is my solution: Format textview and measure. After that set width of textview with 1 pixel add to the width measured.

    TextView textView = new TextView(this);
    textView.setText("Text blah blah");
    textView.setTypeface(typeface, Typeface.BOLD_ITALIC)
    textView.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
    textView.setLayoutParams(new LayoutParams(textView.getMeasuredWidth() + 1, LayoutParams.WRAP_CONTENT));

Working for me. Hope these help.

查看更多
老娘就宠你
5楼-- · 2020-06-30 05:53

You could always create custom TextView that will use for example this font (cause in fact this is a problem with italic type):

Typeface tf = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-LightItalic.ttf");
setTypeface(tf);

More details here.

查看更多
再贱就再见
6楼-- · 2020-06-30 05:55

Ok this is pretty strange but I changed from android:maxLines="1" to android:singleLine="true" and now the text is not getting cut off.

查看更多
狗以群分
7楼-- · 2020-06-30 05:57

I encountered the same problem but with EditText when used some fonts. My solution works also with TextView:

  1. Use padding

  2. As TextView using Canvas.clipRect(float, float, float, float) in onDraw method to crop - create custom Canvas class and override this method (leave it empty).

  3. Next сreate custom TextView and override two methods onSizeChanged and onDraw.

In onSizeChanged create bitmap with the size of TextView and our custom Canvas.

In onDraw first draw in bitmap by passing our custom Canvas to method super.onDraw. After that draw bitmap to target сanvas.

More detailed in my answer to the similar question here

查看更多
登录 后发表回答