最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android TextView實現(xiàn)跑馬燈效果的方法

 更新時間:2020年07月29日 13:27:29   作者:showCar  
這篇文章主要介紹了Android TextView跑馬燈效果實現(xiàn)方法,涉及Android布局文件中相關(guān)屬性的設(shè)置技巧,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

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

代碼實現(xiàn)

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

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方法,并進行判斷,只有設(shè)置了marqueen屬性的才保持選中狀態(tài),否則它就跟普通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屬性要設(shè)置為”marquee”,行數(shù)屬性即singleLine要設(shè)置為true。到此TextView的跑馬燈效果就實現(xiàn)了。

希望本文對大家學(xué)習(xí)Android軟件編程有所幫助。

相關(guān)文章

最新評論

西丰县| 四川省| 屏南县| 扬中市| 托克托县| 灵寿县| 琼结县| 呈贡县| 湖口县| 山东| 雷州市| 双辽市| 平江县| 墨玉县| 辽宁省| 剑阁县| 蒲江县| 沁源县| 治县。| 延安市| 洪泽县| 兴业县| 白沙| 泰兴市| 枝江市| 罗江县| 苗栗市| 东海县| 郯城县| 旺苍县| 越西县| 谢通门县| 壶关县| 集贤县| 东至县| 桂阳县| 德阳市| 蒲江县| 惠东县| 东山县| 永泰县|