我的xml代码是
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:lines="1"
android:singleLine="true"
android:textColor="#ffffff"
android:focusable="true"
android:marqueeRepeatLimit="marquee_forever"
android:ellipsize="marquee"
android:typeface="serif"
android:scrollHorizontally="true"
android:focusableInTouchMode="true"</TextView>
我在onCreate方法中的Activity中设置文本如下
textView.setText("some text ");
textView.setSelected(true);
textView.setFocusable(true);
textView.setFocusableInTouchMode(true);
它正在工作,但当关闭应用程序按下后退按钮并再次重新启动时,文本不会滚动,当我更改活动并进入该文本视图所在的活动时,我不会滚动?我还尝试了自定义文本视图
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.TextView;
public class CustomTextView extends TextView {
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
if(focused)
super.onFocusChanged(focused, direction, previouslyFocusedRect);
}
@Override
public void onWindowFocusChanged(boolean focused) {
if(focused)
super.onWindowFocusChanged(focused);
}
@Override
public boolean isFocused() {
return true;
}
}
但它不能工作
您必须在xml文件中使用android:scrollbars = "horizontal"
,然后使用
tv.setMovementMethod(new ScrollingMovementMethod());
在您的java代码中。
将布局更改为ScrollView
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
</ScrollView
有时您需要在滚动TextView之前请求关注它。
尝试更改此项:
android:layout_width="fill_parent"
到此:
android:layout_width="80dp"
您的代码是正确的,但您必须将文本长度设置为大于设备宽度。