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的話也是這麼解決。

效果圖如下:


發佈了39 篇原創文章 · 獲贊 7 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章