TextView可滑动

最近项目中有个需求textview可以滑动,类似ScrollView,当textview的行数超过显示的行的时候要显示出bar,好了上代码。

在布局文件中设置:

scrollbars="vertical"

<TextView
    android:id="@+id/tv_content"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="15sp"
    android:textColor="@color/ettitle"
    android:lines="4"
    android:ellipsize="end"
    android:layout_margin="10dp"
    android:gravity="top|left"
    android:background="@drawable/add_shape"
    android:padding="5dp"
    android:lineSpacingExtra="3dp"
    android:scrollbars="vertical"
    />
在activity中设置

tvContent.setMovementMethod(new ScrollingMovementMethod());//设置textview可以滑动
tvContent.setScrollbarFadingEnabled(false);//设置scrollbar一直显示
因为我的布局文件外面嵌套有ScrollView所以两个都有滑动特性的控件在一起会有滑动冲突,So加上下面的判断

tvContent.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        tvContent.getParent().requestDisallowInterceptTouchEvent(true);
        return false;
    }
});

轻松解决滑动冲突,同样如果嵌套了微博webview的话也是这么解决。

效果图如下:

TextView可滑动