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

来源:互联网 发布:中电海康研究院 知乎 编辑:程序博客网 时间:2024/05/17 01:59

效果:



java: 用handler循环调用这个方法即可

public static void scroll2Bottom(final ScrollView scroll, final View inner) {Handler handler = new Handler();handler.post(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubif (scroll == null || inner == null) {return;}// 内层高度超过外层int offset = inner.getMeasuredHeight()- scroll.getMeasuredHeight();if (offset < 0) {System.out.println("定位...");offset = 0;}scroll.scrollTo(0, offset);}});}


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

<ScrollView        android:id="@+id/sv_show"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:layout_above="@id/tl_botton"        android:layout_alignParentTop="true"        android:background="#E6E6FA"        android:fadeScrollbars="false"        android:scrollbarAlwaysDrawVerticalTrack="true"        android:scrollbars="vertical" >        <LinearLayout            android:id="@+id/ll_layout"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:background="#00FF00" >            <TextView                android:id="@+id/tv_show"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:background="#FFC0CB"                android:padding="@dimen/padding_medium"                android:scrollbars="vertical"                android:text="@string/hello_world"                tools:context=".MainActivity" />        </LinearLayout>    </ScrollView>




原创粉丝点击