0

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

4

1 回答 1

1

我发现这种行为发生了,然后 MotionEvent 发生在超链接上。因为我只需要可以点击跨度,所以我做了这样的解决方法:

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

}

将此移动方法设置为 TextView 可点击跨度后,仍然会获得 onClick() 事件,但文本流动的错误消失了。

我认为这是因为 LinkMovementMethod 是“一种遍历文本缓冲区中的链接并在必要时滚动的移动方法。支持使用 DPad Center 或 Enter 来单击链接。”

于 2013-02-05T17:18:45.113 回答