Android中使用TextView實(shí)現(xiàn)文字跑馬燈效果
通常情況下我們想實(shí)現(xiàn)文字的走馬燈效果需要在xml文件中這樣設(shè)置
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:singleLine="true" android:ellipsize="marquee" android:focusable="true" android:focusableInTouchMode="true" android:text="@string/lyric" />
大家都懂的就不解釋了。
singleLine :boolean型的是否讓文字只顯示在一行而不是多行顯示
ellipsize:滾動(dòng)效果,里面有(none,start,middle,end,marquee),其中none表示正常顯示文字,即使一行顯示不完全,也無任何效果。star,就是假如文字在一行顯示不完全,在開頭顯示...,同理,end一行的最后一個(gè)文字后面加...,middle就是所有文字顯示在一行,如果文字太多,在中間加入...??赡芪医忉尩牟磺宄蟾啪褪沁@樣 讀者可以自己測(cè)試一下。至于marquee就是文字走馬燈效果啦。
當(dāng)然,如果你只設(shè)置了這些文字還是不會(huì)滾動(dòng)的。還要使TextView獲取焦點(diǎn)。
focusable:是否能夠焦點(diǎn),boolean型的
focusableInTouchMode:boolean型的。
在觸摸模式下是否獲取焦點(diǎn)。
當(dāng)你設(shè)置了這些部署在手機(jī)上,很明顯會(huì)實(shí)現(xiàn)走馬燈效果。效果如下:
可是如果你在這個(gè)Activity實(shí)例中再添加一個(gè)編輯框控件,點(diǎn)擊編輯框后就會(huì)發(fā)現(xiàn)走馬燈效果消失了.
就像這樣
這是為什么呢?
因?yàn)辄c(diǎn)擊編輯框,編輯框會(huì)獲取屏幕焦點(diǎn),由于通常情況下屏幕的焦點(diǎn)只能有一個(gè),TextView失去了焦點(diǎn),也就不會(huì)滾動(dòng)了。這時(shí)候我們要怎么辦呢?
那就欺騙系統(tǒng)唄。告訴它我們的TextView也是有焦點(diǎn)的。沒錯(cuò) 兩個(gè)焦點(diǎn)。
如何做?我們就新建一個(gè)我們自己的TextView唄。
首先我們新建一個(gè)名為MyTextView繼承TextView的類,重寫里面的方法,其中有三個(gè)方法是必須的,就像我們總在MainActivity里面重寫OnCreate方法一樣,作用是什么 我也不清楚。好奇的同學(xué)請(qǐng)百度。哈哈~
我們要知道系統(tǒng)是如何判斷一個(gè)控件是否獲取焦點(diǎn)了呢?
public boolean isFocused() { // TODO Auto-generated method stub return super.isFocused(); }
就是這個(gè)方法。前面說了 我們要欺騙系統(tǒng) 我們的TextViwe是有焦點(diǎn)的。所以我們?cè)谶@個(gè)方法里面一直return true就好了 哈哈
做完了這些別忘記把我們的TextView部署到布局文件中哦
布局代碼:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <com.example.textview.MyTextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:ellipsize="marquee" android:singleLine="true" android:text="@string/lyric" /> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
MainActivity.class
package com.example.textview; import android.os.Bundle; import android.app.Activity; import android.view.Menu; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
MyTextView.class
package com.example.textview; import android.content.Context; import android.util.AttributeSet; import android.view.WindowId.FocusObserver; import android.widget.TextView; public class MyTextView extends TextView{ public MyTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // TODO Auto-generated constructor stub } public MyTextView(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } public MyTextView(Context context) { super(context); // TODO Auto-generated constructor stub } @Override public boolean isFocused() { // TODO Auto-generated method stub return true; } }
對(duì)了 附上效果圖。都看到光標(biāo)在編輯框了~
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時(shí)也希望多多支持腳本之家!
相關(guān)文章
Android實(shí)現(xiàn)簡(jiǎn)易的鬧鐘功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡(jiǎn)易的鬧鐘功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09Android中WebView與Js交互的實(shí)現(xiàn)方法
本文給大家介紹android中webview與js交互的實(shí)現(xiàn)方法,本文介紹的非常詳細(xì),具有參考借鑒價(jià)值,感興趣的朋友一起學(xué)習(xí)2016-05-05android實(shí)現(xiàn)listview分頁(yè)的方法
這篇文章主要介紹了android實(shí)現(xiàn)listview分頁(yè)的方法,涉及Android生成listview列表的相關(guān)技巧,需要的朋友可以參考下2015-05-05Android文本輸入框(EditText)輸入密碼時(shí)顯示與隱藏
這篇文章主要介紹了Android文本輸入框(EditText)輸入密碼時(shí)顯示與隱藏的方法和示例,需要的朋友可以參考下2014-12-12android獲取屏幕高度和寬度的實(shí)現(xiàn)方法
這篇文章主要介紹了android獲取屏幕高度和寬度的實(shí)現(xiàn)方法,較為詳細(xì)的分析了Android獲取屏幕高度和寬度的原理與實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-01-01Android中 webView調(diào)用JS出錯(cuò)的解決辦法
這篇文章主要介紹了Android中 webView調(diào)用JS出錯(cuò)的解決辦法,需要的朋友可以參考下2015-01-01Material Design系列之自定義Behavior支持所有View
這篇文章主要為大家詳細(xì)介紹了Material Design系列之自定義Behavior支持所有View,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09