android 跑馬燈

所謂跑馬燈效果就是當文字超過控件所能容納的空間時,在控件內滾動的效果。

要實現這樣的效果需要在佈局文件中加上:
android:singleLine=”true”
android:ellipsize=”marquee”
android:focusableInTouchMode=”true”
android:focusable=”true”

需要注意的是:layout_width=”"要寫成固定值,不能是wrap_content或者fill_parent,不能比文字長,如果比文字長,就會把後面的文字隱藏掉。另外還可以設置滾動的次數android:marqueeRepeatLimit=”";
android:marqueeRepeatLimit=”marquee_forever”表示一直滾動。

但是這種跑馬燈只有在控件獲得焦點時在能滾動,要想讓控件裏的內容一直滾動就要定製該控件,重寫裏面的三個方法:

package cn.easymobi.application.memorytest;

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.Button;

public class MarqueeButton extends Button {

public MarqueeButton(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
@Override
protected void onFocusChanged(boolean focused, int direction,
Rect previouslyFocusedRect) {
// TODO Auto-generated method stub
if(focused)
super.onFocusChanged(focused, direction, previouslyFocusedRect);
}

@Override
public void onWindowFocusChanged(boolean hasWindowFocus) {
// TODO Auto-generated method stub
if(hasWindowFocus)
super.onWindowFocusChanged(hasWindowFocus);
}
@Override
public boolean isFocused() {
return true;
}

}

下面就是要在佈局文件裏使用這個控件了:

<com.wjl.wigdet.MarqueeButtom
android:layout_width=”216dip”
android:layout_height=”wrap_content”
android:id=”@+id/btSecond”
android:background=”@drawable/button_test2″
android:layout_marginTop=”15dip”
android:text=”@string/calculate”
android:ellipsize=”marquee”
android:gravity=”center”
android:textColor=”@color/white”
android:textStyle=”bold”
android:focusable=”true”
android:marqueeRepeatLimit=”marquee_forever”
android:focusableInTouchMode=”true”
android:scrollHorizontally=”true”
android:singleLine=”true”
android:paddingLeft=”50dip”
android:paddingRight=”50dip”
android:textSize=”20dip”
/>

發佈了24 篇原創文章 · 獲贊 13 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章