Android:TextView 自动滚动(跑马灯) (转)
Android:TextView 自動(dòng)滾動(dòng)(跑馬燈)??
?
TextView實(shí)現(xiàn)文字滾動(dòng)需要以下幾個(gè)要點(diǎn): 1.文字長度長于可顯示范圍:android:singleLine="true" 2.設(shè)置可滾到,或顯示樣式:android:ellipsize="marquee" 3.TextView只有在獲取焦點(diǎn)后才會(huì)滾動(dòng)顯示隱藏文字,因此需要在包中新建一個(gè)類,繼承TextView。重寫isFocused方法,這個(gè)方法默認(rèn)行為是,如果TextView獲得焦點(diǎn),方法返回true,失去焦點(diǎn)則返回false。跑馬燈效果估計(jì)也是用這個(gè)方法判斷是否獲得焦點(diǎn),所以把它的返回值始終設(shè)置為true。 以下轉(zhuǎn)自他人: Java語言:?AlwaysMarqueeTextView 類 public class AlwaysMarqueeTextView extends TextView {public AlwaysMarqueeTextView(Context context) { super(context); }public AlwaysMarqueeTextView(Context context, AttributeSet attrs) { super(context, attrs); }public AlwaysMarqueeTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); }@Override public boolean isFocused() { return true; }?
在布局XML文件中加入這么一個(gè)AlwaysMarqueeTextView,這個(gè)加入方法也是剛剛學(xué)的。
XML語言:?layout.xml <com.examples.AlwaysMarqueeTextView android:id=“@+id/AMTV1″ android:layout_width=“fill_parent” android:layout_height=“wrap_content” android:lines=“1″ android:focusable=“true” android:focusableInTouchMode=“true” android:scrollHorizontally=“true” android:marqueeRepeatLimit=“marquee_forever” android:ellipsize=“marquee” android:background=“@android:color/transparent” />?
ellipsize屬性
設(shè)置當(dāng)文字過長時(shí),該控件該如何顯示。有如下值設(shè)置:”start”—–省略號(hào)顯示在開頭;”end”——省略號(hào)顯示在結(jié)尾;”middle”—-省略號(hào)顯示在中間;”marquee” ——以跑馬燈的方式顯示(動(dòng)畫橫向移動(dòng))
EidtText和textview中內(nèi)容過長的話自動(dòng)換行,使用android:ellipsize與android:singleine可以解決,使只有一行。
EditText不支持marquee
用法如下:
在xml中
android:ellipsize = "end" 省略號(hào)在結(jié)尾android:ellipsize = "start" 省略號(hào)在開頭android:ellipsize = "middle" 省略號(hào)在中間android:ellipsize = "marquee" 跑馬燈android:singleline = "true"?
當(dāng)然也可以用代碼語句
tv.setEllipsize(TextUtils.TruncateAt.valueOf("END"));tv.setEllipsize(TextUtils.TruncateAt.valueOf("START"));tv.setEllipsize(TextUtils.TruncateAt.valueOf("MIDDLE"));tv.setEllipsize(TextUtils.TruncateAt.valueOf("MARQUEE"));tv.setSingleLine(true);?
marqueeRepeatLimit屬性
在ellipsize指定marquee的情況下,設(shè)置重復(fù)滾動(dòng)的次數(shù),當(dāng)設(shè)置為marquee_forever時(shí)表示無限次。
focusable屬性
自己猜測(cè)的,應(yīng)該是能否獲得焦點(diǎn),同樣focusableInTouchMode應(yīng)該是滑動(dòng)時(shí)能否獲得焦點(diǎn)。
組合View的問題:
XML語言:?組合View < LinearLayout xmlns:android =“http://schemas.android.com/apk/res/android” android:orientation =“vertical” android:gravity =“center_vertical” android:background =“@drawable/f_background” android:layout_width =“fill_parent” android:focusable =“true” android:layout_height =“50px” > < TextView android:id =“@+id/info_text” android:focusable =“true” android:layout_width =“fill_parent” android:layout_height =“wrap_content” android:text =“test marquee .. “ android:textColor =“@color/black” android:singleLine =“true” android:ellipsize =“marquee” android:marqueeRepeatLimit =“3″ android:textSize =“18sp” /> < TextView android:id =“@+id/date_text” android:layout_width =“fill_parent” android:layout_height =“wrap_content” android:layout_gravity =“bottom” android:textColor =“@color/gray” android:text =“2010/05/28″ android:textSize =“12sp” /> </ LinearLayout >?
上面示例中2個(gè)TextView組合為一個(gè)View,由于設(shè)置了LinearLayout為focusable而TextView就沒法取得焦點(diǎn)了,這樣 這個(gè)TextView的跑馬燈效果就顯示不出來,就算你也設(shè)置TextView的?android:focusable="true"?也是 沒用的. 這個(gè)時(shí)候就要使用addStatesFromChildren 這個(gè)屬性了,在LinearLayout中設(shè)置這個(gè)屬性,然后設(shè)置TextView的focusable=?"true"?就可以了.關(guān)于 addStatesFromChildren的說明:Sets whether?this?ViewGroup's drawable states?also include its children's drawable states.
?
來自:http://hmifly.blog.163.com/blog/static/1285835072011322352406/
?
轉(zhuǎn)載于:https://www.cnblogs.com/McCa/p/4505211.html
總結(jié)
以上是生活随笔為你收集整理的Android:TextView 自动滚动(跑马灯) (转)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux基本命令(1)管理文件和目录的
- 下一篇: Android 开发工具类 02_Den