TextView 椭圆化不起作用

TextView Ellipsizing not working

我实现了文本视图中文本的自动滚动。

我想再次做同样的事情,但它不起作用。

我也设置了setSelected(true)

XML代码:

<TextView
    android:id="@+id/tvScroll"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_toLeftOf="@+id/ivlogo"
    android:layout_toStartOf="@+id/ivlogo"
    android:ellipsize="marquee"
    android:marqueeRepeatLimit="marquee_forever"
    android:scrollHorizontally="true"
    android:singleLine="true"
    android:text="left goleft left left goleft left left."
    android:textColor="@android:color/holo_blue_dark"
    android:textStyle="bold" />

此 txt 视图采用相对布局。

你的代码没问题,

问题出在您的文字宽度上。修复您的 TextView 宽度或为您的 TextView 设置长值。

例如:-

android:layout_width="50dp" 

android:text="Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum..."

简而言之,使内容大于 TextView 宽度会产生 marquee/auto 滚动效果。

希望对您有所帮助!

谢谢。

试试这个:

YourTextView.requestFocus();
You may use the alternate solution to achieve the same 
Below is the complete code that will help you to put scrolling text hope it will help you.
1.Create a class which extends the Text View e.g

public class ScrollText extends TextView {
    // scrolling feature
    private Scroller mSlr;
    // milliseconds for a round of scrolling
    private int mRndDuration = 5000;
    // the X offset when paused
    private int mXPaused = 0;
    // whether it's being paused
    private boolean mPaused = true;
     //constructor
    public ScrollText(Context context) {
        this(context, null);
        // customize the TextView
        setSingleLine();
        setEllipsize(null);
        setVisibility(INVISIBLE);
    }
   //constructor
    public ScrollText(Context context, AttributeSet attrs) {
        this(context, attrs, android.R.attr.textViewStyle);
        // customize the TextView
        setSingleLine();
        setEllipsize(null);
        setVisibility(INVISIBLE);
    }
    //constructor
    public ScrollText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // customize the TextView
        setSingleLine();
        setEllipsize(null);
        setVisibility(INVISIBLE);
    }
    /**
     * begin to scroll the text from the original position
     */
    public void startScroll() {
        // begin from the very right side
        mXPaused = -1 * getWidth();
        // assume it's paused
        mPaused = true;
        resumeScroll();
    }

    /**
     * resume the scroll from the pausing point
     */
    @SuppressLint("UseValueOf")
    public void resumeScroll() {

        if (!mPaused)
            return;

        // Do not know why it would not scroll sometimes
        // if setHorizontallyScrolling is called in constructor.
        setHorizontallyScrolling(true);

        // use LinearInterpolator for steady scrolling
        mSlr = new Scroller(this.getContext(), new LinearInterpolator());
        setScroller(mSlr);

        int scrollingLen = calculateScrollingLen();
        int distance = scrollingLen - (getWidth() + mXPaused);
        int duration = (new Double(mRndDuration * distance * 1.500000
                / scrollingLen)).intValue();

        setVisibility(VISIBLE);
        mSlr.startScroll(mXPaused, 0, distance, 0, duration);
        invalidate();
        mPaused = false;
    }

    /**
     * calculate the scrolling length of the text in pixel
     * 
     * @return the scrolling length in pixels
     */
    private int calculateScrollingLen() {
        TextPaint tp = getPaint();
        Rect rect = new Rect();
        String strTxt = getText().toString();
        tp.getTextBounds(strTxt, 0, strTxt.length(), rect);
        int scrollingLen = rect.width() + getWidth();
        rect = null;
        return scrollingLen;
    }

    /**
     * pause scrolling the text
     */
    public void pauseScroll() {
        if (null == mSlr)
            return;

        if (mPaused)
            return;

        mPaused = true;

        // abortAnimation sets the current X to be the final X,
        // and sets isFinished to be true
        // so current position shall be saved
        mXPaused = mSlr.getCurrX();

        mSlr.abortAnimation();
    }

    @Override
    /*
     * override the computeScroll to restart scrolling when finished so as that
     * the text is scrolled forever
     */
    public void computeScroll() {
        super.computeScroll();

        if (null == mSlr)
            return;

        if (mSlr.isFinished() && (!mPaused)) {
            this.startScroll();
        }
    }

    public int getRndDuration() {
        return mRndDuration;
    }

    public void setRndDuration(int duration) {
        this.mRndDuration = duration;
    }

    public boolean isPaused() {
        return mPaused;
    }
}

2. in the onCreate() of main activity paste the code shown below

      ScrollText scrolltext = (ScrollText) findViewById(R.id.footer_tv_scroll);
       String textToscroll = "YOUR TEXT"
       scrolltext.setText(s);
        scrolltext.setTextColor(Color.BLACK);
       scrolltext.startScroll();


3. inside your xml place the code shown below

       <<YOUR PACKAGE NAME>.ScrollText
        android:id="@+id/footer_tv_scroll"
        android:layout_width="650dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@+id/footer_iv_logo"
        android:textSize="35sp"
        android:textStyle="bold" />

hope this works 
Cheers!