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:17
 <TextView
  android:ellipsize="marquee"
  android:singleLine="true"
  .../>

must call in code

textView.setSelected(true);
查看更多
君临天下
3楼-- · 2018-12-31 19:18

To have your own scroll speed and flexibility to customize marquee properties, use the following:

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ellipsize="marquee"
    android:fadingEdge="horizontal"
    android:lines="1"
    android:id="@+id/myTextView"
    android:padding="4dp"
    android:scrollHorizontally="true"
    android:singleLine="true"
    android:text="Simple application that shows how to use marquee, with a long text" />

Within your activity:

private void setTranslation() {
        TranslateAnimation tanim = new TranslateAnimation(
                TranslateAnimation.ABSOLUTE, 1.0f * screenWidth,
                TranslateAnimation.ABSOLUTE, -1.0f * screenWidth,
                TranslateAnimation.ABSOLUTE, 0.0f,
                TranslateAnimation.ABSOLUTE, 0.0f);
        tanim.setDuration(1000);
        tanim.setInterpolator(new LinearInterpolator());
        tanim.setRepeatCount(Animation.INFINITE);
        tanim.setRepeatMode(Animation.ABSOLUTE);

        textView.startAnimation(tanim);
    } 
查看更多
低头抚发
4楼-- · 2018-12-31 19:19

Working Code:

<TextView
    android:id="@+id/scroller"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ellipsize="marquee"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:singleLine="true"
    android:text="Some veryyyyy long text with all the characters that cannot fit in screen, it so sad :( that I will not scroll"
    android:textAppearance="?android:attr/textAppearanceLarge" />
查看更多
裙下三千臣
5楼-- · 2018-12-31 19:19

I've encountered the same problem. Amith GC's answer(the first answer checked as accepted) is right, but sometimes textview.setSelected(true); doesn't work when the text view can't get the focus all the time. So, to ensure TextView Marquee working, I had to use a custom TextView.

public class CustomTextView extends TextView {
    public CustomTextView(Context context) {
        super(context);
    }
    public CustomTextView(Context context, AttributeSet attrs) {
        super(context, attrs);

    }

    public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

    }


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

And then, you can use the custom TextView as the scrolling text view in your layout .xml file like this:

<com.example.myapplication.CustomTextView
            android:id="@+id/tvScrollingMessage"
            android:text="@string/scrolling_message_main_wish_list"
            android:singleLine="true"
            android:ellipsize="marquee"
            android:marqueeRepeatLimit ="marquee_forever"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:scrollHorizontally="true"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:background="@color/black"
            android:gravity="center"
            android:textColor="@color/white"
            android:textSize="15dp"
            android:freezesText="true"/>

NOTE: in the above code snippet com.example.myapplication is an example package name and should be replaced by your own package name.

Hope this will help you. Cheers!

查看更多
余生请多指教
6楼-- · 2018-12-31 19:19

android:focusable="true" and android:focusableInTouchMode="true" are essential....

Because I tested all others without these lines and the result was no marquee. When I add these it started to marquee..

查看更多
只若初见
7楼-- · 2018-12-31 19:20

Very Simple working code:

For infinitely scrolling text

            <TextView
            android:id="@+id/textView_News_HeadLine"
            style="@style/black_extra_large_heading_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="8dp"
            android:ellipsize="marquee"
            android:marqueeRepeatLimit="-1"
            android:singleLine="true"
            android:text="HeadLine: Banglawash To be Continued" />

& you should must write from your activity

textView.setSelected(true);
查看更多
登录 后发表回答