1. 程式人生 > >android中TextView多行文字滾動的實現及單行走馬燈實現

android中TextView多行文字滾動的實現及單行走馬燈實現

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"/>