Android水平滚动文字无法正常工作

时间:2015-01-09 08:04:35

标签: android

在我的应用中,我希望在图像下方滚动很长的文字。我查看了StackOverflow示例并进入了以下布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" >

<ImageView
    android:id="@+id/mImageView"
    android:contentDescription="@string/description"
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_weight=".9" 
    android:background="#000000"
    android:layout_gravity="center"
    android:layout_alignParentRight="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_alignParentBottom="true" />

   <vds.playlistmanager.ScrollingTextView
            android:id="@+id/scrollingtextView1"
            android:layout_width="wrap_content"
            android:layout_height="0dip"
            android:layout_weight=".1" 
            android:background="#ff0000"
            android:paddingLeft="10dip"
            android:paddingRight="10dip"
            android:textSize="25dip"
            android:textStyle="bold"
            android:focusable="true"
            android:focusableInTouchMode="true"
         android:text="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"                
            android:singleLine="true"
            android:scrollHorizontally="true"
            android:marqueeRepeatLimit="marquee_forever"
            />  

并定义了班级

public class ScrollingTextView extends TextView implements Runnable {
private static final float DEFAULT_SPEED = 15.0f;
private Scroller scroller;
private float speed = DEFAULT_SPEED;
private boolean continuousScrolling = true;

public ScrollingTextView(Context context) {
    super(context);
    setup(context);
}
public ScrollingTextView(Context context, AttributeSet attributes) {
    super(context, attributes);
    setup(context);
}
private void setup(Context context) {
    scroller = new Scroller(context, new LinearInterpolator());
    setScroller(scroller);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    super.onLayout(changed, left, top, right, bottom);
    if (scroller.isFinished()) {
        scroll();
    }
}

@Override
 public boolean isFocused() {
    return true;
 }  
 private void scroll() {
    int viewHeight = getHeight();
    int viewWidth = getWidth();
    int visibleHeight = viewHeight - getPaddingBottom() - getPaddingTop();
    int visibleWidth = viewWidth - getPaddingLeft() - getPaddingRight();
    int lineHeight = getLineHeight();
    //int offsetY = -1 * visibleHeight;
    int offsetY = 0;
    //int distanceY = visibleHeight + getLineCount() * lineHeight;
    int distanceY = 0;
    int distanceX = visibleWidth / 2;
    int duration = (int) (distanceX * speed);
    scroller.startScroll(0, offsetY, distanceX, distanceY, duration);
    if (continuousScrolling) {
        post(this);
    }
}
@Override
public void run() {
    if (scroller.isFinished()) {
        scroll();
    } else {
        post(this);
    }
}
public void setSpeed(float speed) {
    this.speed = speed;
}
public float getSpeed() {
    return speed;
}
public void setContinuousScrolling(boolean continuousScrolling) {
    this.continuousScrolling = continuousScrolling;
}
public boolean isContinuousScrolling() {
    return continuousScrolling;
}
}

问题出现在文本滚动中,但只显示了适合TextView内部的文字,而不是整篇文字,我所期待的。

1 个答案:

答案 0 :(得分:0)

为什么使用自定义Textview而不是使用Textview内置方法。你可以很容易地完成它。只需尝试下面的代码。

在你的xml中

<TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:lines="1"
        android:ellipsize="marquee"
        android:fadingEdge="horizontal"
        android:marqueeRepeatLimit="marquee_forever"
        android:scrollHorizontally="true"
        android:textColor="#ff4500"
        android:singleLine="true"
        android:text="Simple application that shows how to use marquee, with a long text  like Once upon a time there was a king in an old kingdom.He was very brave and kind." />

在您的班级中添加以下代码

        TextView tv = (TextView) findViewById(R.id.textView1);
        tv.setSelected(true);