带有滚动条和 maxHeight 的 Android TextView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2478615/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android TextView with scrollbars and maxHeight
提问by alex2k8
I need to set the TextView maximum height (either using maxHeight or maxLines). And if there are more lines of text the scrollbars should be shown. What markup should I use for this?
我需要设置 TextView 最大高度(使用 maxHeight 或 maxLines)。如果有更多行文本,则应显示滚动条。我应该为此使用什么标记?
Initial idea was to wrap TextView with ScrollView, but ScrollView has no maxHeight attribute.
最初的想法是用 ScrollView 包裹 TextView,但 ScrollView 没有 maxHeight 属性。
回答by alex2k8
Layout:
布局:
<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>
Code:
代码:
TextView textView = (TextView)findViewById(R.id.text_view);
textView.setMovementMethod(ScrollingMovementMethod.getInstance());
Note:
笔记:
The android:textColor="@android:color/secondary_text_dark_nodisable" is used to avoid text fade out when textView is touched.
android:textColor="@android:color/secondary_text_dark_nodisable" 用于避免触摸 textView 时文本淡出。