欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

代碼實(shí)現(xiàn)

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

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方法,并進(jìn)行判斷,只有設(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>

要注意兩點(diǎn)ellipsize屬性要設(shè)置為”marquee”,行數(shù)屬性即singleLine要設(shè)置為true。到此TextView的跑馬燈效果就實(shí)現(xiàn)了。

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

相關(guān)文章

  • Android GridLayout使用案例詳解

    Android GridLayout使用案例詳解

    這篇文章主要介紹了Android GridLayout使用案例詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • Android實(shí)現(xiàn)合并生成分享圖片功能

    Android實(shí)現(xiàn)合并生成分享圖片功能

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)合并生成分享圖片功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-03-03
  • Android中判斷網(wǎng)絡(luò)是否連接實(shí)例詳解

    Android中判斷網(wǎng)絡(luò)是否連接實(shí)例詳解

    這篇文章主要介紹了Android中判斷網(wǎng)絡(luò)是否連接實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-01-01
  • Android ContentResolver使用說明

    Android ContentResolver使用說明

    這篇文章主要介紹了Android ContentResolver使用說明,需要的朋友可以參考下
    2016-01-01
  • Android編程之?dāng)?shù)據(jù)庫的創(chuàng)建方法詳解

    Android編程之?dāng)?shù)據(jù)庫的創(chuàng)建方法詳解

    這篇文章主要介紹了Android編程之?dāng)?shù)據(jù)庫的創(chuàng)建方法,結(jié)合實(shí)例形式分析了Android數(shù)據(jù)庫創(chuàng)建的步驟、實(shí)現(xiàn)技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下
    2017-08-08
  • 直接可用的Android studio學(xué)生信息管理系統(tǒng)

    直接可用的Android studio學(xué)生信息管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了直接可用的Android studio學(xué)生信息管理系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • Flutter3.7新增Menu菜單組件的使用教程分享

    Flutter3.7新增Menu菜單組件的使用教程分享

    之前Flutter的菜單選擇、下拉菜單的支持非常簡單且不友好,對(duì)于非常常見的下拉菜單選擇功能是需要自己自定義實(shí)現(xiàn),今天看到Flutter3.7版本新增了一系列菜單的組件,馬上來試試
    2023-01-01
  • Android中獲取手機(jī)屏幕大小的方法

    Android中獲取手機(jī)屏幕大小的方法

    這篇文章主要介紹了Android中獲取手機(jī)屏幕大小的方法,Android開發(fā)需要獲得屏幕的寬高,本文為大家解析 Android中如何獲取手機(jī)屏幕大小,需要的朋友可以參考下
    2015-12-12
  • Android中的SpannableString與SpannableStringBuilder詳解

    Android中的SpannableString與SpannableStringBuilder詳解

    這篇文章主要給大家介紹了關(guān)于Android中SpannableString與SpannableStringBuilder的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-10-10
  • Android編程之SurfaceView學(xué)習(xí)示例詳解

    Android編程之SurfaceView學(xué)習(xí)示例詳解

    這篇文章主要介紹了Android編程之SurfaceView學(xué)習(xí)示例,結(jié)合實(shí)例分析了SurfaceView的功能、使用方法與注意事項(xiàng),具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-10-10

最新評(píng)論