国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android TextView實現跑馬燈效果的方法

2020-04-11 10:53:45
字體:
來源:轉載
供稿:網友

本文為大家分享一個非常簡單但又很常用的控件,跑馬燈狀態的TextView。當要顯示的文本長度太長,又不想換行時用它來顯示文本,一來可以完全的顯示出文本,二來效果也挺酷,實現起來超級簡單,所以,何樂不為。先看下效果圖:

代碼實現

TextView自帶了跑馬燈功能,只要把它的ellipsize屬性設置為marquee就可以了。但有個前提,就是TextView要處于被選中狀態才能有效果,看到這,我們就很自然的自定義一個控件,寫出以下代碼:

public class MarqueeTextView extends TextView { public MarqueeTextView(Context con) {  super(con); } public MarqueeTextView(Context context, AttributeSet attrs) {  super(context, attrs); } public MarqueeTextView(Context context, AttributeSet attrs, int defStyle) {  super(context, attrs, defStyle); } @Override public boolean isFocused() {  // TODO Auto-generated method stub  if(getEditableText().equals(TruncateAt.MARQUEE)){   return true;  }  return super.isFocused(); }}

重寫了isFocused方法,并進行判斷,只有設置了marqueen屬性的才保持選中狀態,否則它就跟普通TextView一樣。接下來就可以直接使用了,看下布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <FrameLayout  android:id="@+id/titlebar_layout"  android:layout_width="match_parent"  android:layout_height="wrap_content"  android:background="#39ac69" >  <LinearLayout   android:layout_width="match_parent"   android:layout_height="50dp"   android:background="#ffffff"   android:gravity="center_vertical"   android:orientation="horizontal" >   <ImageView    android:id="@+id/home_location_iv"    android:layout_width="25dp"    android:layout_height="27dp"    android:layout_marginLeft="10dp"    android:scaleType="fitXY"    android:src="@drawable/icon_place" />   <com.lxj.marqueetextview.MarqueeTextView    android:id="@+id/home_location_tv"    android:layout_width="0dp"    android:layout_height="wrap_content"    android:layout_marginLeft="10dp"    android:layout_marginRight="10dp"    android:layout_weight="1"    android:ellipsize="marquee"    android:focusable="true"    android:focusableInTouchMode="true"    android:gravity="center"    android:marqueeRepeatLimit="marquee_forever"    android:scrollHorizontally="true"    android:singleLine="true"    android:text="正在定位..."    android:textColor="#39ac69"    android:textSize="18sp" />   <ImageView    android:id="@+id/home_search_iv"    android:layout_width="25dp"    android:layout_height="27dp"    android:layout_marginRight="10dp"    android:scaleType="fitXY"    android:src="@drawable/icon_place" />  </LinearLayout></FrameLayout></LinearLayout>

要注意兩點ellipsize屬性要設置為”marquee”,行數屬性即singleLine要設置為true。到此TextView的跑馬燈效果就實現了。

希望本文對大家學習Android軟件編程有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 光泽县| 崇义县| 泾阳县| 宜黄县| 蒲江县| 芮城县| 佛学| 通山县| 湘潭县| 巫溪县| 上栗县| 海门市| 沈阳市| 环江| 鲁甸县| 湘乡市| 布尔津县| 昌黎县| 六枝特区| 东乡县| 金秀| 饶阳县| 石首市| 旬阳县| 朔州市| 池州市| 济南市| 晋城| 高台县| 白朗县| 大理市| 晴隆县| 岳池县| 年辖:市辖区| 大关县| 萨迦县| 介休市| 屏东市| 沿河| 林芝县| 广昌县|