android中TextView多行文本滚动的实现及单行走马灯实现

来源:互联网 发布:网络推广的主要方法 编辑:程序博客网 时间:2024/05/16 18:52

TextView当设定行数是单行的时候可以实现走马灯效果,但有时候多行的时候如何实现文本滚动呢,一般方法:

NO.1setMovementMethod方法
textView赋值前,调用如下方法即可实现文本滚动,此时是没有滚动条的。 注意一定要在setText之前调用setMovementMethod方法

TextView textView = (TextView)findViewById(R.id.tv_test);   textView.setMovementMethod(ScrollingMovementMethod.getInstance());textView.setText("abc");

NO.2ScrollView实现方式
在ScrollView标签中嵌入一个TextView标签,但局限是ScrollView只能有一个直接的子类布局。

<ScrollView      android:layout_width="match_parent"      android:layout_height="wrap_content" >     <TextView          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:text="@string/test"/>  </ScrollView>  

当TextView单行的时候实现走马灯效果只需要在xml中添加几个属性:

 <TextView        android:id="@+id/name1"        android:layout_margin="10dp"        android:layout_height="wrap_content"        android:layout_width="match_parent"        android:gravity="center"        android:textSize="35dp"        android:textColorLink="#c91b1b"        android:textColor="#172f78"        android:text="fdsf"        android:maxLines="1"        android:layout_weight="2"       <!--添加下面几个属性即可-->         android:ellipsize="marquee"        android:singleLine="true"        android:clickable="true"        android:focusable="true"        android:focusableInTouchMode="true"/>
0 0
原创粉丝点击