Android Textview marquee not working.

2019-07-09 00:54发布

问题:

How come the following code not working. I have something similar at other part of the layout and it works. But this part does not work. Any idea?

    <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"                
            android:orientation="vertical" >

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:singleLine="true"
                android:ellipsize="marquee"
                android:marqueeRepeatLimit="marquee_forever"                   
                android:scrollHorizontally="true" 
                android:paddingLeft="50dip" 
                android:paddingRight="50dip"
                android:focusable="true"  
                android:focusableInTouchMode="true"
                android:layout_gravity="center_vertical" 
                android:id="@+id/statusTvBottom"
                android:text="Status: "                            
                android:layout_marginLeft="5dp">
            </TextView> 
         </LinearLayout>

回答1:

First, the text has to be longer than the "box" in order to marquee, if it fits-no marquee. I use this code:

<TextView
        android:id="@+id/availability"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="marquee"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:gravity="center_vertical|center_horizontal"
        android:marqueeRepeatLimit="marquee_forever"
        android:scrollHorizontally="true"
        android:singleLine="true"
        android:textColor="#343434"
        android:textSize="@dimen/text_size_medium"
        android:textStyle="bold|italic" />

Setting android:focusable="false" and android:focusableInTouchMode="false" allows it to marquee without focus.



回答2:

First, you can try this:

textview.setSelected(true); 

But it might not 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!