2012年4月19日 星期四

[Android]TextView Marquee 跑馬燈文字


說明:
1      當你想要以TextView一行的方式來顯示文字,可是有的文字太長卻無法全部顯示時,這時可以指定TextView的相關屬性來讓過長的文字以跑馬燈的方式顯示。
2       TextView的相關屬性說明:
2.1     android:ellipsize="marquee" : 過長的文字以【marquee:跑馬燈模式】顯示。
2.2     android:focusable="true" : TextView可以取得焦點。
2.3     android:focusableInTouchMode="true" : 指定在觸控模式下取得焦點。
2.4     <重點> focusable="true" focusableInTouchMode="true" 時跑馬燈文字才會有效動作。
2.5     android:marqueeRepeatLimit="marquee_forever" : 跑馬燈的循環次數設為marquee_forever (無限循環)
2.6     android:singleLine="true" : 指定TextView內的文字以單行顯示。

<TextViewMarquee.java>
   1: package com.android.textviewmarquee;
   2:  
   3: import android.app.Activity;
   4: import android.os.Bundle;
   5:  
   6: public class TextViewMarquee extends Activity {
   7:     /** Called when the activity is first created. */
   8:     @Override
   9:     public void onCreate(Bundle savedInstanceState) {
  10:         super.onCreate(savedInstanceState);
  11:         setContentView(R.layout.main);
  12:     }
  13: }

<main.xml>
   1: <?xml version="1.0" encoding="utf-8"?>
   2: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   3:     android:layout_width="fill_parent"
   4:     android:layout_height="fill_parent"
   5:     android:orientation="vertical" >
   6:  
   7:     <TextView
   8:         android:layout_width="fill_parent"
   9:         android:layout_height="wrap_content"
  10:         android:ellipsize="marquee"
  11:         android:focusable="true"
  12:         android:focusableInTouchMode="true"
  13:         android:marqueeRepeatLimit="marquee_forever"
  14:         android:padding="5sp"
  15:         android:singleLine="true"
  16:         android:text="@string/strMessage"
  17:         android:textSize="30sp" />
  18:  
  19: </LinearLayout>

<strings.xml>
   1: <?xml version="1.0" encoding="utf-8"?>
   2: <resources>
   3:  
   4:     <string name="hello">Hello World, TextViewMarquee!</string>
   5:     <string name="app_name">TextViewMarquee</string>
   6:     <string name="strMessage">這是 TextView Marquee 跑馬燈文字測試 >>>>>>>></string>
   7:  
   8: </resources>


沒有留言:

張貼留言