12

我想在 TextView 中反转选取框的方向。默认情况下,文本从右向左移动,我希望它从左向右移动。我怎样才能做到这一点?

4

8 回答 8

10

我想出了一个非常简单易行的方法来做到这一点。根据我们的选择,我制作了一个选框效果,可以在两个方向上移动。所以,这里是诀窍:

我在 Horizo​​ntalScrollView 中使用了 TextView。我以编程方式控制它的滚动。我使用以下方法获得了文本长度:

scroll_pos = (int)myTextView.getLayout().getLineWidth(0);

然后我使用 Handler 并递归调用它,直到达到滚动限制。在这个处理程序中,我让 Horizo​​ntalScrollView 滚动到某个位置:

Handler hHandler = new Handler()
{
    @Override
    public void handleMessage(Message msg)
    {
        hScroll.scrollTo(scroll_pos, 0);
        scroll_pos--;
        if(scroll_pos >= 0)
            hHandler.sendEmptyMessage(0);
    }       
};

就这样,从左到右的平滑选取框。干杯!

于 2011-08-22T09:23:25.447 回答
8

另一种简单的方法是使用 HTML,您也可以轻松改变方向direction="Left"

<html><body><FONT COLOR="#000000" ><marquee id="mrqSlogan"  direction="Left" style="width: auto;" >text your</marquee></FONT></body></html>

并传递给 WebView

webView.loadDataWithBaseURL(null, yourhtmltext, "text/html" , null, null);
于 2012-10-04T12:29:18.243 回答
6

最后我在这里找到了最好的解决方案: https ://github.com/Torob/MarqueeView

使用这个库:

  1. 只需将此 java 文件 ( MarqueeView.java ) 添加到您的项目 java 文件夹中
  2. 在您的 xml 布局中,将您的 TextView 放在这个 MarqueeView 中,如下所示:

      <your.packagename.MarqueeView
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:id="@+id/horizontalScrollView"
           android:scrollbars="none"
           android:visibility="visible"
           android:clickable="false"> 
       <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/test"
            android:id="@+id/scrollingtext"
            android:singleLine="true"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:paddingRight="25dp"
            android:paddingLeft="25dp" />
       </your.packagename.MarqueeView>
    
于 2016-02-28T10:44:27.823 回答
1

您正在寻找的功能目前似乎不可用。

您可以从 TextView.java 中的源代码创建自己的反向选取框文本视图,但其中有很多对“选取框”的引用。我数了超过 50 个,因此可能需要一些时间来反转滚动方向。

我认为一些双向语言支持可能会让你欺骗 textview 从左到右滚动,但 Android 似乎不太支持 RTL 语言。

目前,您唯一的选择是接受选取框的方向或创建您自己的支持您的功能的 TextView 类。

我会从第 3810 - 3815 行看这一部分

  if (mMarquee != null && mMarquee.isRunning()) {
        canvas.translate(-mMarquee.mScroll, 0.0f);
    }

在 mMarquee 变为:

  if (mMarquee != null && mMarquee.isRunning()) {
        canvas.translate(mMarquee.mScroll, 0.0f);
    }

显然,您需要进行额外的更改,但这会为您指明正确的方向(字面意思)。

于 2011-07-19T12:10:01.020 回答
1

我建议您使用这种行为创建自己的组件。

像这样: ViewFlipper 有一个 TextView 作为它的孩子(显示你的文本)。

flipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.marquee_in));
flipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.marquee_out));

选框:

<?xml version="1.0" encoding="utf-8"?>
<set
    xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:fromXDelta="0%p"
        android:toXDelta="-100%p"
        android:duration="1500"/>
</set>

选框:

<?xml version="1.0" encoding="utf-8"?>
<set
    xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:fromXDelta="100%p"
        android:toXDelta="0%p"
        android:duration="1500"/>
</set>

您必须稍微调整持续时间以使其看起来像“原生”动画。:-)

于 2011-08-08T00:21:03.417 回答
0

这是解决方案:

在布局文件中设置:

<TextView
android:id="@+id/textId"
android:layout_width="fill_parent"
android:layout_height="wrap_content"    
android:textColor="#337700"
android:textSize="20px"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:singleLine="true"
android:textStyle="bold"
android:text="Download Form for Different Vehiecle Purposes ....!!!">
</TextView>

并将其设置在活动中:

    tv = (TextView) findViewById(R.id.textId);
    tv.setSelected(true);

在我的情况下,这是从右向左移动文本。

于 2011-07-15T10:33:38.923 回答
0

对于每个人都可以使用的从右到左的文本:试试这个:

        TextView tv = (TextView) findViewById(R.id.txt);
    Rect bounds = new Rect();
    Paint textPaint = tv.getPaint();
    String text = tv.getText().toString();
    textPaint.getTextBounds(text, 0, text.length(), bounds);
    int width = bounds.width();
    LinearLayout.LayoutParams lp = (LayoutParams) tv.getLayoutParams();
    lp.width = width + 100;
            int startX = 300;
    TranslateAnimation ta = new TranslateAnimation(startX, lp.width, 0, 0);
    ta.setDuration(20000);
    ta.setRepeatCount(-1);
    tv.setAnimation(ta);
于 2013-10-10T06:34:55.847 回答
0

设置布局方向 android:layoutDirection="rtl" android:textDirection="rtl"

 <android.support.v7.widget.AppCompatTextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="12dp"
            android:layout_marginRight="12dp"
            android:layout_weight="1"
            android:layoutDirection="rtl"
            android:textDirection="rtl"
            android:ellipsize="marquee"
            android:marqueeRepeatLimit="marquee_forever"
            android:singleLine="true"
            android:text=" ......... گپ دوستان بیسیبییبب "
            android:textColor="@color/colorPrimaryDark"
            android:textSize="@dimen/font_size_xlarge" />
于 2018-11-17T12:21:25.177 回答