android:scrollbar该属性的值必须设为vertical android:text只有比较长的文本才可以滚动
android:scrollbarStyle 该属性表示滚动条的位置,如果设为outsideOverlay,滚动条会在文字的右侧显示,如果设为insideOverlay,滚动条会在右侧文字上显示,也就是说会覆盖文字的一部分。android:scrollbarFadeDuration 该属性表示滚动条从出现到消失的时间,单位是毫秒
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:id="@+id/textview" android:layout_width="fill_parent" android:layout_height="wrap_content" android:singleLine="false" android:maxLines="8" android:background="#FFF" android:textColor="#000" android:textSize="20dp" android:layout_margin="10dp" android:scrollbars="vertical" android:text="@string/big_text" android:scrollbarStyle="outsideOverlay" android:padding="10dp" android:scrollbarFadeDuration="2000" /> </RelativeLayout>
main函数如下
package com.luohaibo.testdemo04; import android.os.Bundle; import android.app.Activity; import android.text.method.ScrollingMovementMethod; import android.view.Menu; import android.widget.TextView; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView textView = (TextView) findViewById(R.id.textview); textView.setMovementMethod(ScrollingMovementMethod.getInstance()); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }