android:TextView實現文字走馬燈效果(欺騙系統擷取持久的焦點)

來源:互聯網
上載者:User

標籤:

通常情況下我們想實現文字的走馬燈效果需要在xml檔案中這樣設定

    <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:滾動效果,裡面有(none,start,middle,end,marquee),其中none表示正常顯示文字,即使一行顯示不完全,也無任何效果。star,就是假如文字在一行顯示不完全,在開頭顯示...,同理,end一行的最後一個文字後面加...,middle就是所有文字顯示在一行,如果文字太多,在中間加入...。可能我解釋的不清楚,大概就是這樣 讀者可以自己測試一下。至於marquee就是文字走馬燈效果啦。

當然,如果你只設定了這些文字還是不會滾動的。還要使TextView擷取焦點。

focusable:是否能夠焦點,boolean型的

focusableInTouchMode:boolean型的。

在觸摸模式下是否擷取焦點。

當你設定了這些部署在手機上,很明顯會實現走馬燈效果。效果如下:


可是如果你在這個Activity執行個體中再添加一個編輯框控制項,點擊編輯框後就會發現走馬燈效果消失了.

就像這樣


這是為什麼呢?

因為點擊編輯框,編輯框會擷取螢幕焦點,由於通常情況下螢幕的焦點只能有一個,TextView失去了焦點,也就不會滾動了。這時候我們要怎麼辦呢?

那就欺騙系統唄。告訴它我們的TextView也是有焦點的。沒錯 兩個焦點。

如何做?我們就建立一個我們自己的TextView唄。

首先我們建立一個名為MyTextView繼承TextView的類,重寫裡面的方法,其中有三個方法是必須的,就像我們總在MainActivity裡面重寫OnCreate方法一樣,作用是什麼 我也不清楚。好奇的同學請百度。哈哈~

我們要知道系統是如何判斷一個控制項是否擷取焦點了呢?

public boolean isFocused() {// TODO Auto-generated method stubreturn super.isFocused();}

就是這個方法。前面說了 我們要欺騙系統 我們的TextViwe是有焦點的。所以我們在這個方法裡面一直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 {@Overrideprotected 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}@Overridepublic boolean isFocused() {// TODO Auto-generated method stubreturn true;}}

對了 附上。都看到游標在編輯框了~



android:TextView實現文字走馬燈效果(欺騙系統擷取持久的焦點)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.