讓 TextView 自帶滾動條

最近在做一個小軟件,從中想設置 TextView 帶滾動條,有點類似於 多行的 EditText, 可以自由的移動Cursor。好了,轉入正題。實現步驟如下:

一、Xml代碼

  1. <TextView  
  2.     android:id="@+id/textview"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="wrap_content"  
  5.     android:singleLine="false"  
  6.     android:maxLines="5"  
  7.     android:scrollbars="vertical"  
  8.     />

二、還需要在代碼中設置 TextView 相應的方法

  1. TextView textView = (TextView)findViewById(R.id.text_view);   
  2. textView.setMovementMethod(ScrollingMovementMethod.getInstance()); 

好了,大功告成。

 

附:

  順便講下 TextView 自動滾動的實現方法,下面介紹兩種方法:

一、在代碼中實現:

     textView .setEllipsize(TextUtils.TruncateAt.MARQUEE);
     textView .setSingleLine(true);
     textView .setMarqueeRepeatLimit(6);

二、在XML中實現:

 <TextView android:id="@+id/TextView01" android:layout_width="wrap_content"
  android:layout_height="wrap_content" android:singleLine="true"
  android:text="dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"
  android:marqueeRepeatLimit="marquee_forever" android:ellipsize="marquee"
  android:scrollHorizontally="true" android:width="150dip"></TextView>

一切OK,當 textView 獲取焦點後,就會自動滾動。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章