简体   繁体   English

android TextView中的文本溢出

[英]Overflowing of text in android TextView

I am experiencing a rather strange bug with TextView on Android. 我在Android上使用TextView遇到一个相当奇怪的错误。 The text can flow over TextView's top border at random times then user interacts with it. 文本可以随机流过TextView的顶部边框,然后用户与其交互。 It looks like this happens when TextView changes it height with animation (it expands and shrinks on click). 当TextView随动画更改其高度(单击时会放大和缩小)时,似乎会发生这种情况。

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 因此,它的外观如下(查看使用android dev工具启用的边框绘图,并且我已经用红色标记了TextView的边框) http://s7.postimage.org/c1ynrbwjv/so_full.png

I've tried calling invalidate() ( postInvalidate() either), requestLayout() and resetting gravity of TextView. 我试过调用invalidate()postInvalidate() ), requestLayout()并重置TextView的重力
I also tried to rewrite expanding and shrinking animation, to use getLayoutParams().height to set height, instead of setMaxHeight() . 我还尝试重写扩展和收缩动画,以使用getLayoutParams()。height设置高度,而不是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). 最后,我将TextView放入LinearLayout(将TextView的高度设置为layout_height =“ wrap_content”),然后展开/缩小LinearLayout而不是TextView(因此TextView的边界不会直接改变)。 But that, as you can guess, didn't help. 但是,您可以猜测,这没有帮助。

TextView layout (BoardPostText extends TextView): TextView布局(BoardPostText扩展了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 TextView的代码https://gist.github.com/d47e8eafb1bcff7c8db1

I've found that such behaviour occured then MotionEvent happened over hyperlink. 我发现发生此类行为,然后在超链接上发生了MotionEvent。 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. 将此移动方法设置为TextView后,可单击范围仍会发生onClick()事件,但文字流动的错误消失了。

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. " 我认为这是因为LinkMovementMethod是“一种移动方法,该方法将遍历文本缓冲区中的链接并在必要时滚动 。支持使用DPad Center或Enter单击链接。”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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