Android的自动水平滚动的TextView水平、Android、TextView

2023-09-12 08:50:44 作者:臸噂寳

我想实现一个单行文本视图,将自动滚动显示。但不幸的是我无法得到它的工作。所述AutoScrollTextView是内部的LinearLayout(宽度和高度= FILL_PARENT)声明。类基本上使用一个Handler调用自身由一个给定的量来滚动。我已经简化了code只能说明,应该由5个像素每秒可滚动文本视图。

日志输出是正确的,getScrollX()方法返回一个适当scrollX位置。

如果我不叫 requestLayout(),没有被绘制。 无效()不起任何作用。

会有人有线索?

 公共类AutoScrollTextView扩展TextView的{

    公共AutoScrollTextView(上下文的背景下,ATTRS的AttributeSet,诠释defStyle){
        超(背景下,ATTRS,defStyle);
        setSingleLine();
        setEllipsize(空);
        的setText(单行文本认为,如果滚动自动文本太长,以适应小部件);
    }

    //开始滚动从原来的位置的文本
    公共无效startScrolling(){
        scrollHandler.sendEmptyMessage(0);
    }

    私人处理程序的scrollHandler =新的处理程序(){
        私有静态最终诠释REFRESH_INTERVAL = 1000;

        公共无效的handleMessage(信息MSG){
            scrollBy(5,0);
            requestLayout();
            Log.debug(滚动至+ getScrollX()+像素);
            sendEmptyMessageDelayed(0,REFRESH_INTERVAL);
        }
    };
}
 

解决方案 android的TextView自动滚动

如果您不需要子类的TextView ,你可以在你的布局试试这个文件:

 <的TextView
        机器人:文本=单行文本视图自动滚动,如果文字太长,无法在窗口小部件
        机器人:单线=真
        机器人:ellipsize =金字招牌
        机器人:marqueeRepeatLimit =marquee_forever
        机器人:可聚焦=真
        机器人:focusableInTouchMode =真
        机器人:scrollHorizo​​ntally =真
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT/>
 

I am trying to implement a single-line text view that will scroll automatically. But I unfortunatly cannot get it to work. The AutoScrollTextView is declared inside a LinearLayout (width and height = fill_parent). The class basically uses a Handler that calls itself to scroll by a given amount. I have simplified the code to only show a text view that should be scrolling by 5 pixels every second.

The log output is correct, the getScrollX() method returns the appropriate scrollX position.

If I don't call requestLayout(), nothing gets drawn. invalidate() has no effect.

Would anybody have a clue?

public class AutoScrollTextView extends TextView {

    public AutoScrollTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setSingleLine();
        setEllipsize(null);
        setText("Single-line text view that scrolls automatically if the text is too long to fit in the widget");
    }

    // begin to scroll the text from the original position
    public void startScrolling() {
        scrollHandler.sendEmptyMessage(0);
    }

    private Handler scrollHandler = new Handler() {
        private static final int REFRESH_INTERVAL = 1000;

        public void handleMessage(Message msg) {
            scrollBy(5, 0);
            requestLayout();
            Log.debug("Scrolled to " + getScrollX() + " px");
            sendEmptyMessageDelayed(0, REFRESH_INTERVAL);
        }
    };
}

解决方案

If you don't need to sub-class the TextView, you can try this in your layout file:

    <TextView
        android:text="Single-line text view that scrolls automatically if the text is too long to fit in the widget" 
        android:singleLine="true" 
        android:ellipsize="marquee"
        android:marqueeRepeatLimit ="marquee_forever"
        android:focusable="true"
        android:focusableInTouchMode="true" 
        android:scrollHorizontally="true"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"/>