1. 程式人生 > >TextView取消自動換行並設定水平滾動

TextView取消自動換行並設定水平滾動

      假如需要顯示一段程式碼,通常程式碼一行的長度超出了手機螢幕的寬度,這時候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了。