android TextView 帶滾動條,和ScrollView 用法(暫時覺得ScrollView滑動速度比較快)


本來是想做一個顯示文字信息的,當文字很多時View的高度不能超過一個固定的值,當文字很少時View的高度小於那個固定值時,按View的高度顯示。因為ScrollView沒有maxHeight,無法滿足需求,只好另找方法了。

 

View本身是可以設置ScrollBar,這樣就不一定需要依賴ScrollView了。TextView有個屬性maxLine,這樣也就滿足了需求了,只要設置一個TextView帶ScrollBar的,然后設置maxLine就可以了。

 

 

Xml代碼 復制代碼  收藏代碼
  1. <TextView  
  2.     android:id="@+id/text_view"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="wrap_content"  
  5.     android:singleLine="false"  
  6.     android:maxLines="10"  
  7.     android:scrollbars="vertical"  
  8.     />  
<TextView
    android:id="@+id/text_view"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:singleLine="false"
    android:maxLines="10"
    android:scrollbars="vertical"
    />

還需要在代碼了設置TextView可以滾動。

Java代碼 復制代碼  收藏代碼
  1. TextView textView = (TextView)findViewById(R.id.text_view);   
  2. textView.setMovementMethod(ScrollingMovementMethod.getInstance());  

------------------------------------------------------------------------------------------------------------------------------------------

 

如果用ScrollView ,代碼如下,<ScrollView            
           android:layout_width="745dip"
           android:layout_height="520dip"
           android:id="@+id/mBtnRe2"
           android:layout_marginTop="30dip"
           android:layout_marginLeft="30dip"  
           android:fadingEdge="none"     
           >
            <TextView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:id="@+id/mTxtContent"
                android:textSize="25sp"
                android:textColor="#000000"
                android:fadingEdge="none"
                />
           </ScrollView>  

 

 

--------------------------------------------------------------------------------------------------------------------------------------------

ScrollView可以調整滑動速度,自己實現ScrollView

**

    •      * 快/慢滑動ScrollView
    •      * @author 農民伯伯
    •      *
    •      */
    •     public class SlowScrollView extends ScrollView {
    •    
    •         public SlowScrollView(Context context, AttributeSet attrs, int defStyle) {
    •             super(context, attrs, defStyle);
    •         }
    •    
    •         public SlowScrollView(Context context, AttributeSet attrs) {
    •             super(context, attrs);
    •         }
    •    
    •         public SlowScrollView(Context context) {
    •             super(context);
    •         }
    •    
    •         /**
    •          * 滑動事件
    •          */
    •         @Override
    •         public void fling(int velocityY) {
    •             super.fling(velocityY / 4);
    •         }
    •     }
    • 復制代碼代碼說明:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM