繁体   English   中英

通过程序在TextView中水平滚动

[英]Horizontal scrolling in TextView by program

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

使用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 light,那很好,但是您应该意识到可能存在的问题。

使用此代码

<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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM