TextView Marquee not working [duplicate]

2018-12-31 19:13发布

This question already has an answer here:

I have tried to use marquee and its not working here is my code, please let me know where im going wrong

<TextView
   android:text="lunch 20.00 | Dinner 60.00 | Travel 60.00 | Doctor 5000.00 | lunch 20.00 | Dinner 60.00 | Travel 60.00 | Doctor 5000.00"
   android:id="@+id/TextView02"
   android:layout_width="200dip"
   android:layout_height="wrap_content"
   android:marqueeRepeatLimit="marquee_forever"
   android:ellipsize="marquee"
   android:singleLine="true"
   android:focusable="true"
   android:inputType="text"
   android:maxLines="1">
</TextView>

i am using android SDK 2.0.1

21条回答
低头抚发
2楼-- · 2018-12-31 19:22

I had gone through this situation where textview marquee was not working. However follow this and I am sure it will work. :)

<TextView
         android:id="@+id/tv_marquee"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:ellipsize="marquee"
         android:focusable="true"
         android:focusableInTouchMode="true"
         android:freezesText="true"
         android:maxLines="1"
         android:scrollHorizontally="true"
         android:text="This is a sample code of marquee and it works"/>

and programmatically add these 2 lines...

tvMarquee.setHorizontallyScrolling(true);
tvMarquee.setSelected(true);

tvMarquee.setSelected(true) is required incase if any one of the view is already focused and setSelected will make it work. No need to use.

android:singleLine="true"

it is deprecated and above codes works.

查看更多
残风、尘缘若梦
3楼-- · 2018-12-31 19:22

I have created a custom class AlwaysMarqueTextView

public class AlwaysMarqueeTextView extends TextView
{
    protected boolean a;

    public AlwaysMarqueeTextView(Context context)
    {
        super(context);
        a = false;
    }

    public AlwaysMarqueeTextView(Context context, AttributeSet attributeset)
    {
        super(context, attributeset);
        a = false;
    }

    public AlwaysMarqueeTextView(Context context, AttributeSet attributeset, int i)
    {
        super(context, attributeset, i);
        a = false;
    }

    public boolean isFocused()
    {
        return a || super.isFocused();
    }

    public void setAlwaysMarquee(boolean flag)
    {
        setSelected(flag);
        setSingleLine(flag);
        if(flag)
        setEllipsize(TruncateAt.MARQUEE);
        a = flag;
    }

    @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);
    }
}

And you can startMarquee when desire.. like

//textView.setSelected(true); No need of Selection..
textview.setAlwaysMarquee(true); 
查看更多
初与友歌
4楼-- · 2018-12-31 19:26

Yes, marquee_forever also work in case of fixed width for TextView. (e.g. android:layout_width="120dp")

Must required attributes are:

  1. android:focusable="true"
  2. android:focusableInTouchMode="true"
  3. android:singleLine="true" // if it's missing text appear in multiple line.

Working code:

<TextView
                android:id="@+id/mediaTitleTV"
                android:layout_width="220dp"
                android:layout_height="wrap_content"
                android:ellipsize="marquee"
                android:focusable="true"
                android:focusableInTouchMode="true"
                android:marqueeRepeatLimit="marquee_forever"
                android:singleLine="true"
                android:text="Try Marquee, it works with fixed size textview smoothly!" />
查看更多
心情的温度
5楼-- · 2018-12-31 19:28

These attributes must be included in the textview tag in order to allow scrolling.

Everything else is optional.

android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="fill_parent"
android:ellipsize="marquee"
查看更多
牵手、夕阳
6楼-- · 2018-12-31 19:28

I'm working with minSDK=14 and was curious what set of these variations would work. I ended up with:

android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:singleLine="true"

in addition to other formatting stuff. I didn't need scrollHoriontally, focusable, or focusableInTouchMode.

This set did require a call to

setSelected(true)

What I find interesting is that singleLine has allegedly been deprecated, with a recommendation to replace it with maxLines = 1. Except - when I do that, that change alone stops the text from scrolling. One would hope that when singleLine eventually bites the dust, that all its current behavior will be triggered by maxLines...

查看更多
一个人的天荒地老
7楼-- · 2018-12-31 19:31
android:singleLine="true"
android:ellipsize="marquee"

are the only required attributes and scrolling even works with layout_weight defined with layout_width=0dp

here is some sample code:

<TextView 
            android:id="@+id/scroller"
            android:singleLine="true"
            android:ellipsize="marquee"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="#FFFFFF"
            android:text="Some veryyyyy long text with all the characters that cannot fit in screen, it so sad :( that I will not scroll"
            android:layout_marginLeft="4dp"
            android:layout_weight="3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            />

But what is most important is implicitely or explicitely TextView should get selected.

You can do this with:

TextView txtView=(TextView) findViewById(R.id.scroller);
txtView.setSelected(true);
查看更多
登录 后发表回答