Android 跑马灯的方法

转自 http://jsf.iteye.com/blog/680257

http://www.devdiv.com/home.php?mod=space&uid=31865&do=blog&id=11360

http://blog.csdn.net/knlnzhao/article/details/7996969

http://www.2cto.com/kf/201207/144776.html


只记录一种方法了(文字长度超过控件长度,永久滚动;没有超出,不滚动)

走马灯的效果主要是通过android:singleLine,android:ellipsize,android:marqueeRepeatLimit属性来配置的。

android:singleLine=true ,表示使用单行文字,多行文字也就无所谓使用Marquee效果了。

android:marqueeRepeatLimit: 设置走马灯滚动的次数。

android:ellipsize: 设置了文字过长时如何切断文字,可以有none, start,middle, end, 如果使用走马灯效果则设为marquee.

但是Android的缺省行为是在控件获得Focus时才会显示走马灯效果,本例使用的Button,在某个Button获得焦点时Button上的文字才显示。

当有些情况下需要是文字一直滚动以引起用户注意,这是可以使用派生TextView,重载isFocused 这个方法。

package com.example.looptext;

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

/**
 * 单行文本跑马灯控件
 * 
 * @author admin
 *
 */

public class ScrollForeverTextView extends TextView {

	 public ScrollForeverTextView(Context context) {
	 super(context);
	 // TODO Auto-generated constructor stub
	 }
	
	 public ScrollForeverTextView(Context context, AttributeSet attrs) {
	 super(context, attrs);
	 }
	
	 public ScrollForeverTextView(Context context, AttributeSet attrs,int
	 defStyle) {
	 super(context, attrs, defStyle);
	 }
	
	 @Override
	 public boolean isFocused() {
	 return true;
	 }

}
xml

<?xml version="1.0" encoding="UTF-8"?>
<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.looptext.ScrollForeverTextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:ellipsize="marquee"
        android:focusable="true"
        android:marqueeRepeatLimit="marquee_forever"
        android:singleLine="true"
        android:text="12345678901234567890"
        android:textColor="#ff0000"
        android:textSize="30px" />

</LinearLayout>





  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值