假如需要显示一段代码,通常代码一行的长度超出了手机屏幕的宽度,这时候TextView默认会选择自动换行,代码由一行变成了两行,很不美观。
所以,这篇文章记录如何取消自动换行并且设置TextView为水平滚动。
布局代码:
<TextView
android:id="@+id/article_content_code_TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="horizontal"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#abcabc" />
java代码:
textView = (TextView) convertView
.findViewById(R.id.article_content_code_TextView);
textView.setText(element.getCode());
textView.setMovementMethod(ScrollingMovementMethod
.getInstance());
textView.setHorizontallyScrolling(true); // 不让超出屏幕的文本自动换行,使用滚动条
textView.setFocusable(true);
这样就OK了。