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