android TextView多行文本始终显示滚动条并自动定位到底部

效果:



java: 用handler循环调用这个方法即可,而且传入的两个参数分别是ScrollView,LinearLayout

[java]  view plain copy
  1. public static void scroll2Bottom(final ScrollView scroll, final View inner) {  
  2.         Handler handler = new Handler();  
  3.         handler.post(new Runnable() {  
  4.   
  5.             @Override  
  6.             public void run() {  
  7.                 // TODO Auto-generated method stub  
  8.                 if (scroll == null || inner == null) {  
  9.                     return;  
  10.                 }  
  11.                 // 内层高度超过外层  
  12.                 int offset = inner.getMeasuredHeight()  
  13.                         - scroll.getMeasuredHeight();  
  14.                 if (offset < 0) {  
  15.                     System.out.println("定位...");  
  16.                     offset = 0;  
  17.                 }  
  18.                 scroll.scrollTo(0, offset);  
  19.             }  
  20.         });  
  21.     }  


XML: 关键设置scrollview的:android:fadeScrollbars="false"表示始终显示垂直滚动条

[html]  view plain copy
  1. <ScrollView  
  2.         android:id="@+id/sv_show"  
  3.         android:layout_width="fill_parent"  
  4.         android:layout_height="fill_parent"  
  5.         android:layout_above="@id/tl_botton"  
  6.         android:layout_alignParentTop="true"  
  7.         android:background="#E6E6FA"  
  8.         android:fadeScrollbars="false"  
  9.         android:scrollbarAlwaysDrawVerticalTrack="true"  
  10.         android:scrollbars="vertical" >  
  11.   
  12.         <LinearLayout  
  13.             android:id="@+id/ll_layout"  
  14.             android:layout_width="fill_parent"  
  15.             android:layout_height="wrap_content"  
  16.             android:background="#00FF00" >  
  17.   
  18.             <TextView  
  19.                 android:id="@+id/tv_show"  
  20.                 android:layout_width="wrap_content"  
  21.                 android:layout_height="wrap_content"  
  22.                 android:background="#FFC0CB"  
  23.                 android:padding="@dimen/padding_medium"  
  24.                 android:scrollbars="vertical"  
  25.                 android:text="@string/hello_world"  
  26.                 tools:context=".MainActivity" />  
  27.         </LinearLayout>  
  28.     </ScrollView>                                                                                                                   
  29. 而且需要注意的是:调用方法是传入的两个参数分别是ScrollView,LinearLayout的实例!
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值