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

Android中使用TextView實(shí)現(xiàn)文字跑馬燈效果

 更新時(shí)間:2017年04月06日 09:32:47   作者:甄情  
本文主要介紹了Android中使用TextView實(shí)現(xiàn)文字走馬燈效果的方法解析。具有很好的參考價(jià)值。下面跟著小編一起來看下吧

通常情況下我們想實(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)文章

最新評(píng)論