【发布时间】:2011-07-25 05:57:34
【问题描述】:
我正在尝试实现一个自动滚动的单行文本视图。但不幸的是,我无法让它发挥作用。 AutoScrollTextView 在 LinearLayout 中声明(宽度和高度 = fill_parent)。该类基本上使用一个处理程序,该处理程序调用自身以滚动给定数量。我已经简化了代码,只显示一个应该每秒滚动 5 个像素的文本视图。
日志输出正确,getScrollX()方法返回合适的scrollX位置。
如果我不调用requestLayout(),则不会绘制任何内容。 invalidate() 无效。
有人知道吗?
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);
}
};
}
【问题讨论】:
-
通常对于滚动文本,它应该是焦点。您是否尝试过专注于您的布局?还是对视图说 setSelected()?
-
是的,尝试从 Activity 和自定义小部件中的 Handler 执行此操作。