文本视图中的文本不滚动甚至我在android中的xml中设置了ellipsize =“marquee”

时间:2012-03-06 10:51:29

标签: android

我的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;
}

}

但它无法正常工作

5 个答案:

答案 0 :(得分:5)

您必须在xml文件中使用android:scrollbars = "horizontal",然后使用

tv.setMovementMethod(new ScrollingMovementMethod()); 

在你的java代码中。

答案 1 :(得分:1)

将您的布局更改为ScrollView

<ScrollView 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
</ScrollView 

答案 2 :(得分:1)

尝试更改此内容: 机器人:layout_width = “FILL_PARENT”

到这个

机器人:layout_width = “80dp”

如果有效,请告诉我。 如果没有,请不要downvote;)

答案 3 :(得分:1)

有时你需要在TextView上滚动之前请求焦点。

答案 4 :(得分:0)

您的代码是正确的,但您必须将文字长度设置为大于设备宽度。