在android上面让TextView 过多的文字实现有滚动条

来源:互联网 发布:linux 修改 oracle sid 编辑:程序博客网 时间:2024/05/17 08:13

在android上面让TextView 过多的文字实现有滚动条,之前想简单了以为设置TextView的属性就可以实现,结果还是需要ScrollView配合使用,才能达到滚动条的效果有两种方式实现,

一种是代码写java的layout

Java代码

  1. RelativeLayout.LayoutParams param new RelativeLayout.LayoutParams(80,80);   
  2. //初始化滚动条控件   
  3. ScrollView scrollView =new ScrollView(this);   
  4. scrollView.setScrollContainer(true);   
  5. scrollView.setFocusable(true);   
  6. //初始化文字控件   
  7. TextView textView new TextView(this);   
  8. textView.setHorizontallyScrolling(true);   
  9. textView.setText("走情感路线,哈哈哈,\n 走情感路线,哈哈哈");   
  10.   
  11. scrollView.addView(textView);   
  12. main_view.addView(scrollView, param);  
 

另一种则用layout.xml来实现

 

Xml代码
  1. <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"  
  2.         android:layout_width="fill_parent" android:layout_height="fill_parent"    
  3. android:scrollbars="vertical" android:fadingEdge="vertical">  
  4.         <TextView android:layout_width="fill_parent" android:layout_height="wrap_content"    
  5. android:id="@+id/text_view" android:textColor="#071907" android:paddingTop="5dip" />  
  6. </ScrollView>  
原创粉丝点击