我有一个 TextView ,其中包含单行阿拉伯语(从右到左)文本。文本内容随时间增加,最后(最左边)的部分应该总是出现。水平滚动条初始设置为向右,随着文本增加自动向左滚动。
以下代码在 2.3 Android 手机上运行良好。 这个来自layout.xml
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="none"
android:fadingEdge="horizontal"
android:gravity="right"
android:scrollHorizontally="true"
android:scrollbars="horizontal"
android:singleLine="true"
android:text="@string/txt_start"
android:textAppearance="?android:attr/textAppearanceLarge" />
我在 Activity 的 onCreate() 方法中有这个
tv = (TextView) findViewById(R.id.textView1);
tv.setMovementMethod(new ScrollingMovementMethod());
问题是 android 4.0+ 手机上 TextView 的行为不一致。水平滚动条最初没有放在右边,所以 TextView 最初是空白的!如果我尝试滚动空白的 textView,文本就会显示出来。添加额外文本时,滚动条不会滚动以显示新文本,但如果我手动滚动文本,它就会出现。
我搜索了 stackoverflow,并尝试添加以下属性但没有成功。
android:focusable="true"
android:focusableInTouchMode="true"
最佳答案
在我想使用 android:gravity="right"
动画滚动 TextView 的情况下,这些解决方案都不起作用。有帮助的简单解决方法是将 TextView 重力保持在左侧,但将其与 RelativeLayout 容器内的右侧对齐:
<RelativeLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<AutoScrollingTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:gravity="left"
android:singleLine="true"
android:ellipsize="none">
</RelativeLayout>
关于android - 水平滚动的 TextView 在 Android 版本中显示不一致的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15461100/