如何反转TextView的选取框的方向

Kha*_*war 12 android textview android-animation android-layout

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

Kha*_*war 10

我想出了一个非常简单易行的方法.根据我们的选择,我做了两个方向移动的选框效果.所以,这是诀窍:

我在Horizo​​ntalScrollView中使用了TextView.我以编程方式控制其滚动.我使用了以下文字:

scroll_pos = (int)myTextView.getLayout().getLineWidth(0);
Run Code Online (Sandbox Code Playgroud)

然后我使用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);
    }       
};
Run Code Online (Sandbox Code Playgroud)

在这里,从左到右是一个平滑的选框.干杯!

  • @khawar - 它在行上给出"java.lang.NullPointerException":scroll_pos =(int)myTextView.getLayout().getLineWidth(0); "有什么想法吗? (2认同)

Par*_*han 8

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

<html><body><FONT COLOR="#000000" ><marquee id="mrqSlogan"  direction="Left" style="width: auto;" >text your</marquee></FONT></body></html>
Run Code Online (Sandbox Code Playgroud)

并传递给WebView

webView.loadDataWithBaseURL(null, yourhtmltext, "text/html" , null, null);
Run Code Online (Sandbox Code Playgroud)


Sae*_*esh 6

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

感谢torob.ir(这是一个波斯最好的价格搜索引擎)

使用这个库:

  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>
    
    Run Code Online (Sandbox Code Playgroud)