Android TextView实现跑马灯

TextView实现跑马灯的效果:
例子一:

这个例子可以解决给一个TextView实现跑马灯的效果,但是不能解决给所有的TextView实现跑马灯的效果。

<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="这个项目的作用就是让这个单行的很长很长的单独一行的TextView实现跑马灯的效果。。。。。。" />

但是这种方法在有两个TextView的时候不能给两个TextView同时实现跑马灯的效果。
原因是:TextView默认的isFocused()方法默认只能给一个对象实现focused。
解决的办法是新建一个类MarqueeTextView继承TextView,让他的isFocused()方法返回true。(注意:子类MarqueeTextView需要实现父类的所有3个构造函数,不然会有问题)。

package com.example.textviewhorseracelamp;

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

public class MarqueeTextView extends TextView {

    public MarqueeTextView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public MarqueeTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }

    public MarqueeTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

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

MarqueeTextView.java

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <com.example.textviewhorseracelamp.MarqueeTextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:text="这个项目的作用就是让这个单行的很长很长的单独一行的TextView实现跑马灯的效果。。。。。。" />

    <com.example.textviewhorseracelamp.MarqueeTextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:text="这个项目的作用就是让这个单行的很长很长的单独一行的TextView实现跑马灯的效果。。。。。。" />

</RelativeLayout>

activity_main.xml

效果:

补充:像素
 px不能根据分辨率进行缩放
 dp,sp,dip可以根据分辨率进行缩放显示(现在以dp作为标准)
 sp更多的用在文字的缩放

时间: 2024-08-03 15:25:19

Android TextView实现跑马灯的相关文章

android TextView实现跑马灯效果(字体滚动)

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" androi

Android:TextView文字跑马灯的效果实现

解决TextView文字显示不全的问题. 简单设置跑马灯的效果: <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:ellipsize="marquee" android:focusable="true" androi

android textview 实现跑马灯效果

跑马灯效果最重要的就是四个属性,分别是: android:ellipsize="marquee" android:focusable="true" android:focusableInTouchMode="true" android:singleLine="true" 控件的宽度,不一定是具体的值,可以是math_parent,如果想让textview中的文字滚动的话,那里面内容的长度肯定是要大于控件的长度的,不然滚动还有啥

Android界面(1) 使用TextView实现跑马灯效果

方法一:(只能实现单个TextView的跑马灯效果)在TextView添加以下控件 android:singleLine="true"只能单行,超出的文字显示为"..." android:ellipsize="marquee"省略号没有,但没有跑马灯效果 android:focusable="true" android:focusableInTouchMode="true" 方法二:(可以实现多行TextV

android使用TextView实现跑马灯的效果(1)

android使用TextView实现跑马灯的效果 1.activity_main.xml <?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"

ListView 中的TextView实现跑马灯效果

案例:怎么样在一个ListView中含有TextView的item中实现字母滚动呢.这个在一些特定的场合经常用得到.如下图,当焦点位于某个item的时候其内容就自动滚动显示 要实现这样的效果,废话不多说直接上代码: adapter对应的layout 对应的listView的layout 切记千万不要在TextView中加上android:focusable="true" view plain <LinearLayout android:orientation="vert

android 自定义文字跑马灯 支持拖拽,按住停止滚动,自定义速度

android的textview自带跑马灯效果,一般使用足够了.不过也有不一般的情况,所以我实现了一个自定义textview控件,用来针对这种不一般情况下的跑马灯效果实现. ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57

TextView的跑马灯效果(AS开发实战第二章学习笔记)

TextView的跑马灯效果跑马灯用到的属性与方法说明singleLine 指定文本是否单行显示ellipsize 指定文本超出范围后的省略方式focusable 指定是否获得焦点,跑马灯效果要求设置为truefocusableInTouchMode 指定在触摸时是否获得焦点,跑马灯效果要求设置为true省略方式的取值说明start 省略号在开头middle 省略号在中间end 省略号在末尾marquee 跑马灯显示另外,我还找到了一个marqueeRepeatLimit的属性,其中可以用mar

Android 使用TextView实现跑马灯效果

前言 我们在开发中经常会遇到一个小问题.比如下面一个小例子: 这个文字太长,单行中导致无法全部显示出来,这就是今天要实现的功能. 当然,百度中也有很多这种解决方案. 其中有一种,例如: <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:singleLine="true" android:ellipsize=&q