Vin*_*rat 58 android scroll textview
我正在尝试实现一个自动滚动的单行文本视图.但我不幸的是无法让它发挥作用.AutoScrollTextView在LinearLayout(width和height = fill_parent)中声明.该类基本上使用一个Handler,它调用自己按给定的量滚动.我已将代码简化为仅显示应每秒滚动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);
}
};
}
Run Code Online (Sandbox Code Playgroud)
raj*_*ath 191
如果您不需要对其进行子类化TextView
,可以在布局文件中尝试:
<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"/>
Run Code Online (Sandbox Code Playgroud)
此外,在您的代码中使用以下内容:
findViewById(R.id.serviceColorCode).setSelected(true);
Run Code Online (Sandbox Code Playgroud)
[根据评论编辑的答案]
Suj*_*hta 18
在这些xml代码之后由@rajat回答
<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"/>
Run Code Online (Sandbox Code Playgroud)
我们需要设定
TextView tv=(TextView)findViewById(R.id.textview1);
tv.setSelected(true);
Run Code Online (Sandbox Code Playgroud)
这最终使我的工作
alf*_*ibg 16
我的解决方案有效
<TextView
android:id="@+id/titolotxt"
android:layout_width="..."
android:layout_height="..."
android:ellipsize="marquee"
android:gravity="left"
android:marqueeRepeatLimit="marquee_forever"
android:singleLine="true"
android:text="@string/titolo"/>
Run Code Online (Sandbox Code Playgroud)
并且TextView
必须设置选择:
例如textView.setSelected(true);
通过代码onCreate
.
归档时间: |
|
查看次数: |
84088 次 |
最近记录: |