我需要设置TextView的最大高度(使用maxHeight或maxLines)。如果有更多的文本行,应该显示滚动条。我应该使用什么标记来实现这个功能?
最初的想法是使用ScrollView包装TextView,但ScrollView没有maxHeight属性。
最初的想法是使用ScrollView包装TextView,但ScrollView没有maxHeight属性。
页面布局:
<TextView
android:id="@+id/text_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="false"
android:maxLines="3"
android:scrollbars="vertical"
android:textColor="@android:color/secondary_text_dark_nodisable"
>
</TextView>
代码:
TextView textView = (TextView)findViewById(R.id.text_view);
textView.setMovementMethod(ScrollingMovementMethod.getInstance());
注意:
android:textColor="@android:color/secondary_text_dark_nodisable" 用于避免 TextView 在被触摸时出现文本淡化。
TexView.setMovementMethod(new ScrollingMovementMethod())
。这将使文本视图具有滚动功能。 - Cristian