-->

Why Is Marquee Not Working In Widget?

2020-03-24 17:44发布

问题:

I am a noob to android and I am trying to create a widget that uses textview marquee to displays tweets in a ticker fashion. When I set the text in the xml the marquee scrolls properly in the widget. However, when i attempt to set the text programmatically the marquee doesn't scroll. The text is set, but it just doesn't scroll. This behavior is baffling because i used this SO question as my guide. Any help is greatly appreciated.

My Layout

<TextView
        android:id="@+id/ticker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:duplicateParentState="true"
        android:ellipsize="marquee"
        android:fadingEdge="horizontal"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:lines="1"
        android:marqueeRepeatLimit="marquee_forever"
        android:scrollHorizontally="true"
        android:text="Simple application that shows how to use marquee, with a long text for Twitter Feed" > //<--this text scrolls fine

        <requestFocus
            android:duplicateParentState="true"
            android:focusable="true"
            android:focusableInTouchMode="true" />
    </TextView>

My Code

public static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
    //String currentTime =  df.format(new Date());
    String spotgold = String.valueOf(MyActivity.widgetlivespotgold);
    String spotsilver =  String.valueOf(MyActivity.widgetlivespotsilver);
    StringBuilder tickertweets=new StringBuilder();
    for (int i = 0; i < MyActivity.twittername.size(); i++) {
        tickertweets.append(MyActivity.twittername.get(i) + ":" + " " + MyActivityActivity.twittertweet.get(i) + " ");
    }


    RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.widget1);

    updateViews.setTextViewText(R.id.goldspot, spotgold);
    updateViews.setTextViewText(R.id.silverspot, spotsilver);
    updateViews.setTextViewText(R.id.ticker, tickertweets.toString()); //Text is set, but doesn't scroll    
    appWidgetManager.updateAppWidget(appWidgetId, updateViews);
}

回答1:

I guess it is the following Problem:

the Method setText(CharSequence) is resetting the MarqueeRepeatLimit. The Method setText(CharSequence) is a RemotableViewMethod, which means you cann access it from a RemoteView. The setMarqueeRepeatLimit() hasn't this special annotation. I see no way, how you could set the Text and keep your Marquee state inside a RemoteView. Maybe the SetText Method from TextView helps you a little

private void setText(CharSequence text, BufferType type,
                     boolean notifyBefore, int oldlen) {
    if (text == null) {
        text = "";
    }

    if (!mUserSetTextScaleX) mTextPaint.setTextScaleX(1.0f);

    if (text instanceof Spanned &&
        ((Spanned) text).getSpanStart(TextUtils.TruncateAt.MARQUEE) >= 0) {
        setHorizontalFadingEdgeEnabled(true);
        setEllipsize(TextUtils.TruncateAt.MARQUEE);
    }

    int n = mFilters.length;
    for (int i = 0; i < n; i++) {
        CharSequence out = mFilters[i].filter(text, 0, text.length(),
                                              EMPTY_SPANNED, 0, 0);
        if (out != null) {
            text = out;
        }
    }

    if (notifyBefore) {
        if (mText != null) {
            oldlen = mText.length();
            sendBeforeTextChanged(mText, 0, oldlen, text.length());
        } else {
            sendBeforeTextChanged("", 0, 0, text.length());
        }
    }


回答2:

Make sure to use android:singleLine="true" and android:scrollHorizontally="false" (or remove it completely) in your XML. This should do the trick.



回答3:

This might work for you

In your layout:

<TextView
android:id="@+id/TextView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:marqueeRepeatLimit="marquee_forever"
android:text=""/>

In your code:

TextView textView = (TextView)findViewById(R.id.TextView03);
textView.setText(";lkjdf;alkdsfjoiawejrokajdsl;fkadmnsflkamndvklahoreiuaweifjakldsmflkhfgoueyhtoaijpkjdflkahndsofuhyoeia");
textView.setHorizontallyScrolling(true);
textView.setEllipsize(TextUtils.TruncateAt.MARQUEE);
textView.setSingleLine();
textView.setSelected(true);


回答4:

Try this. Set the Marquee span before setting the text.

SpannableString buffer = new SpannableString(tickertweets.toString());
buffer.setSpan(TextUtils.TruncateAt.MARQUEE,0, (tickertweets.length() -1) ,Spannable.SPAN_INCLUSIVE_INCLUSIVE);
updateViews.setTextViewText(R.id.ticker, buffer);


回答5:

you can use this code for scrolling text in textView:

textView.setEllipsize(TextUtils.TruncateAt.MARQUEE);
        textView.setSingleLine();
        textView.setMarqueeRepeatLimit(10);
        textView.setFocusable(true);
        textView.setHorizontallyScrolling(true);
        textView.setFocusableInTouchMode(true);
        textView.requestFocus();