简体   繁体   中英

Overflowing of text in android TextView

I am experiencing a rather strange bug with TextView on Android. The text can flow over TextView's top border at random times then user interacts with it. It looks like this happens when TextView changes it height with animation (it expands and shrinks on click).

So here how it looks (views borders drawing enabled with android dev tools, and I've marked TextView's borders with red ) http://s7.postimage.org/c1ynrbwjv/so_full.png

I've tried calling invalidate() ( postInvalidate() either), requestLayout() and resetting gravity of TextView.
I also tried to rewrite expanding and shrinking animation, to use getLayoutParams().height to set height, instead of setMaxHeight() .
At the end I've put TextView into LinearLayout (setting TextView height to layout_height="wrap_content"), and expanding/shrinking LinearLayout instead of TextView (so TextView's bounds doesn't change directly). But that, as you can guess, didn't help.

TextView layout (BoardPostText extends TextView):

<my.package.view.BoardPostText
    android:id="@+id/postText"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/postTitle"
    android:clickable="true"
    android:linksClickable="true"
    android:longClickable="true"
    android:maxHeight="@dimen/post_image_max_height"
    android:paddingLeft="5dp"
    android:textColor="@color/post_data_text_color"
    android:textSize="@dimen/post_data_text_size"
    android:textColorLink="@color/post_link_color" />

TextView's code https://gist.github.com/d47e8eafb1bcff7c8db1

I've found that such behaviour occured then MotionEvent happened over hyperlink. Because I only need spans to be clickable, I made such workaround:

public static class JustClickableMethod extends LinkMovementMethod {

    public static MovementMethod getInstance() {
        return new JustClickableMethod();
    }

    @Override
    public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) {

        int action = event.getAction() & MotionEvent.ACTION_MASK;

        if( action != MotionEvent.ACTION_UP ) {

            return true;
        }

        return super.onTouchEvent(widget, buffer, event);
    }

}

After setting this movement method to TextView clickable spans still get onClick() event, but bug with text flowing disappears.

I think this is because LinkMovementMethod is "A movement method that traverses links in the text buffer and scrolls if necessary . Supports clicking on links with DPad Center or Enter. "

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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