android TextView添加滚动条

来源:互联网 发布:linux hadoop 单机 编辑:程序博客网 时间:2024/04/29 08:27

android  TextView添加滚动条有两种方法

方法一:

Android默认TextView如果在一屏幕显示不下的话,是不会有滚动条的,解决方法是在<TextView>外面添加<ScrollView>标签;

1.<ScrollView   
2.    android:layout_width="fill_parent"   
3.    android:layout_height="wrap_content" >   
4.   
5.    <TextView   
6.        android:layout_width="fill_parent"   
7.        android:layout_height="wrap_content"   
8.        android:textSize="50dp"   
9.        android:text="1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\12a\n13\n14\n" />   
10.</ScrollView>  


方法二:

经验证, 以下方法可用:

一、Xml代码

<TextView 

    android:id="@+id/textview" 

    android:layout_width="fill_parent" 

    android:layout_height="wrap_content" 

    android:singleLine="false" 

    android:maxLines="5" 

    android:scrollbars="vertical" 

    />

二、还需要在代码中设置 TextView 相应的方法

  1. TextView textView = (TextView)findViewById(R.id.text_view);  

  2. textView.setMovementMethod(ScrollingMovementMethod.getInstance());

注意如果想要滚动条时刻显示, 必须加上以下语句:

textView.setScrollbarFadingEnabled(false);

 

 

 

 




0 0
原创粉丝点击