我遇到了一个“ticker”(水平自动滚动文本)的奇怪问题。
我的应用程序使用片段。其中很多。它基于单个活动,带有操作栏、片段容器和底部代码。
Ticker 正确滚动,从左到右,但每次我更改片段时,我的 Ticker 都会再次初始化(当前水平滚动丢失,它再次从头开始,但没有人告诉他这样做!)。
我正在使用 actionbarsherlock (就像一个魅力!谢谢你杰克沃顿!!)兼容模式。
这是一些代码:
主要活动布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/mainRelative" >
<LinearLayout
android:id="@+id/mainFragmentContainer"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_alignParentTop="true"
android:layout_above="@+id/tickerView1" >
</LinearLayout>
<my.app.views.TickerView
android:id="@+id/tickerView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
</my.app.views.TickerView>
</RelativeLayout>
TICKERVIEW 课程
public class TickerView extends TextView {
private Context context;
public TickerView(Context context) {
super(context);
initialize();
// TODO Auto-generated constructor stub
}
public TickerView(Context context, AttributeSet attrs) {
super(context, attrs);
initialize();
// TODO Auto-generated constructor stub
}
public TickerView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initialize();
// TODO Auto-generated constructor stub
}
public void initialize() {
context = getContext();
String s;
setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
setMarqueeRepeatLimit(-1);
setFocusable(true);
setFocusableInTouchMode(true);
setHorizontallyScrolling(true);
setSingleLine();
setEllipsize(TruncateAt.MARQUEE);
setSelected(true);
setText("sdghaskjghaskgjashgkasjghaksjhgaksjghakjshgkajsghaksjghaksjgh");
}
}
@Override
protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
Log.d("DEBUG", "ON FOCUS CHANGED");
if (focused)
super.onFocusChanged(focused, direction, previouslyFocusedRect);
}
@Override
public void onWindowFocusChanged(boolean focused) {
Log.d("DEBUG", "ON WINDOW FOCUS CHANGED" + (focused ? "FOCUSED" : "NOT FOCUSED"));
if (focused)
super.onWindowFocusChanged(focused);
}
@Override
public boolean isFocused() {
return true;
}
我还尝试了基于 xml 的解决方案(设置具有正确属性的布局文件以滚动文本并从 LinearLayout 扩展我的小部件),但我得到了相同的结果。有任何想法吗?谢谢!