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

來源:互聯網
上載者:User

標籤:

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

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

    <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可以滾動。

    TextView textView = (TextView)findViewById(R.id.text_view);       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);        }    }

 

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.