通过程序在TextView中水平滚动

时间:2014-09-19 08:54:03

标签: java android layout scroll

我有一个单行的TextView,里面有一个文字太长,无法一次显示。现在,当用户执行某些操作时,我希望应用程序可以平滑地水平滚动到文本中的某个位置。因此,滚动是由用户操作启动的,但用户并未决定自己应用程序将滚动到哪个位置(因此我不想通过滑动实现通常的滚动)。我希望有一个功能scrollTo(int position)来完成滚动。

2 个答案:

答案 0 :(得分:0)

使用EditText并为选择属性设置动画。如果这很重要,您可以轻松地将其设置为像TextView一样,例如:

<EditText
    android:enabled="false"
    android:background="@null"
    android:textColor="@android:color/black"
    android:id="@+id/ttt"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="very long text" 
    android:singleLine="true"/>

然后使用属性动画平滑滚动:

ObjectAnimator anim = ObjectAnimator.ofInt(yourEditText, "selection", from, to);
anim.setDuration(duration);
anim.start();

请注意,您不应该为文本使用硬编码颜色,因为在某些手机上背景可能不同(例如,太暗)。如果你有自己的主题或使用Holo点亮它你很好,但你应该意识到可能存在的问题。

答案 1 :(得分:0)

使用此代码

<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/horizontalScrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:text=" This post will help to share your urls and text values into social networks like facebook,twitter and linkedin.in facebook we have to share your urls only, but twitter and linkedin able to share both urls and text." />
</LinearLayout>