我想要在一个文本视图里显示一段文本,但是文字太多,一屏显示不下。我需要让我的TextView可以滚动。我应该怎么做,这是我的代码。
final TextView tv = new TextView(this);
tv.setBackgroundResource(R.drawable.splash);
tv.setTypeface(face);
tv.setTextSize(18);
tv.setTextColor(R.color.BROWN);
tv.setGravity(Gravity.CENTER_VERTICAL| Gravity.CENTER_HORIZONTAL);
tv.setOnTouchListener(new OnTouchListener(){
public boolean onTouch(View v, MotionEvent e)
{
Random r = new Random();
int i = r.nextInt(101);
if (e.getAction() == e.ACTION_DOWN)
{
tv.setText(tips[i]);
tv.setBackgroundResource(R.drawable.inner);
}
return true;
}
});
setContentView(tv);
事实上你不需要使用ScrollView
只要在你的布局的xml文件中设置你的TextView的属性:
android:maxLines = "AN_INTEGER"
android:scrollbars = "vertical"
Then use:
然后在你的代码中用:
yourTextView.setMovementMethod(new ScrollingMovementMethod())
它可以自由的滚动了。
所有的方法中真的有用的是setMovementMethod()。这是用LinearLayout的一个示例代码。
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/tv1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/hello"
/>
</LinearLayout>
WordExtractTest.java
public class WordExtractTest extends Activity {
TextView tv1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv1 = (TextView)findViewById(R.id.tv1);
loadDoc();
}
private void loadDoc(){
String s = "";
for(int x=0;x<=100;x++){
s += "Line: "+String.valueOf(x)+"\n";
}
tv1.setMovementMethod(new ScrollingMovementMethod());
tv1.setText(s);
}
}
这就是我完全用XML实现的
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ScrollView
android:id="@+id/SCROLLER_ID"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:fillViewport="true">
<TextView
android:id="@+id/TEXT_STATUS_ID"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0"/>
</ScrollView>
</LinearLayout>
注意:
1)android:fillViewport="true"和android:layout_weight="1.0"将使得文本视图占据所有可用的空间。
2)当定义Scrollview,不要指定android:layout_height="fill_parent" 否则scrollview 不起作用!(就因为这个浪费了我一个小时的时间)
我赞成的观点:
为了能够在追加文本之后自动滚动到底部,用这个:
mTextStatus = (TextView) findViewById(R.id.TEXT_STATUS_ID);
mScrollView = (ScrollView) findViewById(R.id.SCROLLER_ID);
private void scrollToBottom()
{
mScrollView.post(new Runnable()
{
public void run()
{
mScrollView.smoothScrollTo(0, mTextStatus.getBottom());
}
});
}
textView.post(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
textView.append(line);
final int scrollAmount = textView.getLayout().getLineTop(textView.getLineCount()) - textView.getHeight();
if (scrollAmount > 0)
textView.scrollTo(0, scrollAmount);
else
textView.scrollTo(0, 0);
}
});
这样做过后怎么去掉右边的滑动条?
“niangzhi ”这个用户回答的能实现,但没有具体解释清楚,好像导致了很多人的困惑。现在将解决方法,再详细说一下。**yourTextView.setMovementMethod(new ScrollingMovementMethod()), 这个才是正在让textView实现滚动的方法。** android:maxLines = "AN_INTEGER" 这个属性是设置文本显示地最大行数; android:scrollbars = "vertical" 这个属性是控制滚动条是否显示。
“niangzhi ”这个用户回答的能实现,但没有具体解释清楚,好像导致了很多人的困惑。现在将解决方法,再详细说一下。
yourTextView.setMovementMethod(new ScrollingMovementMethod())
, 这个才是正在让textView实现滚动的方法。
android:maxLines = "AN_INTEGER"
这个属性是设置文本显示地最大行数;
android:scrollbars = "vertical"
这个属性是控制滚动条是否显示。
一句代码就搞定啦,tv.setMovementMethod(ScrollingMovementMethod.getInstance());