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

Android用過(guò)TextView實(shí)現(xiàn)跑馬燈效果的示例

 更新時(shí)間:2017年08月10日 08:29:41   作者:憤斗的小薪  
本篇文章主要介紹了Android用過(guò)TextView實(shí)現(xiàn)跑馬燈效果的示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

以前就遇到過(guò)這個(gè)問(wèn)題,今天重新拾起來(lái)。

跑馬燈效果其實(shí)就是當(dāng)文字超過(guò)TextView控件寬度的時(shí)候,使用滾動(dòng)的方式顯示出來(lái):

方法1:(直接xml搞定)

Android系統(tǒng)中TextView實(shí)現(xiàn)跑馬燈效果,必須具備以下幾個(gè)條件:

1、android:ellipsize=”marquee”;

2、TextView必須單行顯示,且內(nèi)容必須超出TextView寬度;

3、TextView要獲得焦點(diǎn)才能滾動(dòng)。

xml代碼如下:

  <TextView
   android:id="@+id/alarm_location"
   android:layout_width="20dp"
   android:padding="@dimen/space_4"
   android:layout_height="wrap_content"
   app:layout_rowWeight="2"
   app:layout_columnWeight="2"
   android:text="0"
   android:ellipsize="marquee"
   android:focusableInTouchMode="true"
   android:singleLine="true"
   android:focusable="true"/>

其中:ellipsize屬性指的是文字長(zhǎng)度超過(guò)TextView的長(zhǎng)度的時(shí)候的顯示方式,具體參數(shù)有

**Android:ellipsize=”start”—–省略號(hào)顯示在開(kāi)頭 “…pedia”

android:ellipsize=”end”——省略號(hào)顯示在結(jié)尾 “encyc…”

android:ellipsize=”middle”—-省略號(hào)顯示在中間 “en…dia”

android:ellipsize=”marquee”–以橫向滾動(dòng)方式顯示(需獲得當(dāng)前焦點(diǎn)時(shí))**

方法2(自定義控件)

我看了自己以前的實(shí)現(xiàn)方式是這樣,也許在低版本的平臺(tái)上第一種方式適配不好吧。如果達(dá)不到要求可以試試這種方式。

TextVeiwSlide.Java 

package edu.hrbeu.ice.mobilecar.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;

/**
 * @author 編寫(xiě)人: xiaox
 * @date 創(chuàng)建時(shí)間: 2017/1/10
 * @Description 功能描述: 該類(lèi)
 */

public class TextViewSlide extends TextView {
 public TextViewSlide(Context context) {
  super(context);
 }

 public TextViewSlide(Context context, AttributeSet attrs) {
  super(context, attrs);
 }

 @Override
 public boolean isFocused() {
  return true;
 }
}

activity_main.xml 

 <edu.hrbeu.ice.mobilecar.widget.TextViewSlide
  android:id="@+id/alarm_type"
  android:layout_width="@dimen/item_width"
  android:layout_height="wrap_content"
  android:gravity="center_horizontal"
  android:layout_gravity="center_vertical"
  android:layout_weight="1"
  android:padding="8dp"
  android:marqueeRepeatLimit="marquee_forever"
  android:ellipsize="marquee"
  android:scrollHorizontally="true"
  android:focusableInTouchMode="true"
  android:singleLine="true"
  tool:text="asda" />

可以看到第二種方式也就是在自定義控件中獲取了該控件的焦點(diǎn)。感覺(jué)和第一種沒(méi)有區(qū)別。

目前在android7.1.1和android4.4上實(shí)驗(yàn)兩種方法都沒(méi)有問(wèn)題。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論