Android:TextView 自动滚动(跑马灯) (转)

Android:TextView 自动滚动(跑马灯)

TextView实现文字滚动需要以下几个要点:

1.文字长度长于可显示范围:android:singleLine="true"

2.设置可滚到,或显示样式:android:ellipsize="marquee"

3.TextView只有在获取焦点后才会滚动显示隐藏文字,因此需要在包中新建一个类,继承TextView。重写isFocused方法,这个方法默认行为是,如果TextView获得焦点,方法返回true,失去焦点则返回false。跑马灯效果估计也是用这个方法判断是否获得焦点,所以把它的返回值始终设置为true。

以下转自他人:

Java语言: AlwaysMarqueeTextView 类

public class AlwaysMarqueeTextView extends TextView {

public AlwaysMarqueeTextView(Context context) {
super(context);
}

public AlwaysMarqueeTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}

public AlwaysMarqueeTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}

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

在布局XML文件中加入这么一个AlwaysMarqueeTextView,这个加入方法也是刚刚学的。

XML语言: layout.xml

<com.examples.AlwaysMarqueeTextView
android:id=“@+id/AMTV1″
android:layout_width=“fill_parent”
android:layout_height=“wrap_content”
android:lines=“1″
android:focusable=“true”
android:focusableInTouchMode=“true”
android:scrollHorizontally=“true”
android:marqueeRepeatLimit=“marquee_forever”
android:ellipsize=“marquee”
android:background=“@android:color/transparent”
/>

ellipsize属性
设置当文字过长时,该控件该如何显示。有如下值设置:”start”—–省略号显示在开头;”end”——省略号显示在结尾;”middle”—-省略号显示在中间;”marquee” ——以跑马灯的方式显示(动画横向移动)

EidtText和textview中内容过长的话自动换行,使用android:ellipsize与android:singleine可以解决,使只有一行。

EditText不支持marquee

用法如下:

在xml中

android:ellipsize = "end"    省略号在结尾

android:ellipsize = "start"   省略号在开头

android:ellipsize = "middle"     省略号在中间

android:ellipsize = "marquee"  跑马灯

android:singleline = "true"

当然也可以用代码语句

tv.setEllipsize(TextUtils.TruncateAt.valueOf("END"));

tv.setEllipsize(TextUtils.TruncateAt.valueOf("START"));

tv.setEllipsize(TextUtils.TruncateAt.valueOf("MIDDLE"));

tv.setEllipsize(TextUtils.TruncateAt.valueOf("MARQUEE"));

tv.setSingleLine(true);

marqueeRepeatLimit属性
在ellipsize指定marquee的情况下,设置重复滚动的次数,当设置为marquee_forever时表示无限次。

focusable属性
自己猜测的,应该是能否获得焦点,同样focusableInTouchMode应该是滑动时能否获得焦点。

组合View的问题:

XML语言: 组合View

< LinearLayout
xmlns:android =“http://schemas.android.com/apk/res/android”
android:orientation =“vertical”
android:gravity =“center_vertical”
android:background =“@drawable/f_background”
android:layout_width =“fill_parent”
android:focusable =“true”
android:layout_height =“50px” >
< TextView
android:id =“@+id/info_text”
android:focusable =“true”
android:layout_width =“fill_parent”
android:layout_height =“wrap_content”
android:text =“test marquee  .. “
android:textColor =“@color/black”
android:singleLine =“true”
android:ellipsize =“marquee”
android:marqueeRepeatLimit =“3″
android:textSize =“18sp”
/>
< TextView
android:id =“@+id/date_text”
android:layout_width =“fill_parent”
android:layout_height =“wrap_content”
android:layout_gravity =“bottom”
android:textColor =“@color/gray”
android:text =“2010/05/28″
android:textSize =“12sp”
/>
</ LinearLayout >

上面示例中2个TextView组合为一个View,由于设置了LinearLayout为focusable而TextView就没法取得焦点了,这样 这个TextView的跑马灯效果就显示不出来,就算你也设置TextView的 android:focusable="true" 也是 没用的. 这个时候就要使用addStatesFromChildren 这个属性了,在LinearLayout中设置这个属性,然后设置TextView的focusable= "true" 就可以了.关于 addStatesFromChildren的说明:

Sets whether this ViewGroup‘s drawable states also include its children‘s drawable states.

时间: 2024-08-02 02:43:10

Android:TextView 自动滚动(跑马灯) (转)的相关文章

Android TextView 横向滚动(跑马灯效果)

Android TextView 中当文字比较多时希望它横向滚动显示,下面是一种亲测可行的方法. 效果图: 1.自定义TextView,重写isFocused()方法返回true,让自定义TextView一直处于获取焦点状态. package com.example.shen.marqueedemo; import android.content.Context; import android.util.AttributeSet; import android.widget.TextView;

Android 文字自动滚动(跑马灯)效果的两种实现方法[特别好使]

有时候在xml中写的跑马灯效果不滚动:原因有以下 Android系统中TextView实现跑马灯效果,必须具备以下几个条件: 1.android:ellipsize=”marquee” 2.TextView必须单行显示,即内容必须超出TextView大小 3.TextView要获得焦点才能滚动(如果还不行,就要用自定义的TextView控件中重写isFocused()返回true就行[方法代码在下面的AlwaysMarqueeTextView 类],但是遇到新问题就是界面有多个这样的控件显示时当

Android成长日记-仿跑马灯的TextView

在程序设计中有时候一行需要显示多个文字,这时候在Android中默认为分为两行显示,但是对于必须用一行显示的文字需要如何使用呢? --------------------------------------------------------------------- 以下列出解决方法: 1. 新建TextView控件 <TextView android:id="@+id/textView" android:layout_width="wrap_content"

android TextView实现滚动显示效果

在android中,如果设置了TextView控件为单行显示,且显示的文本太长的话,默认情况下会造成显示不全的情况,这种情况下我们需要设置该控件属性如下: <TextView android:id="@+id/tv1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:singleLine="true" and

继承TextView获取焦点实现跑马灯

1 public class MarqueeFocuseTextView extends TextView { 2 3 public MarqueeFocuseTextView(Context context, AttributeSet attrs, 4 int defStyle) { 5 super(context, attrs, defStyle); 6 } 7 8 public MarqueeFocuseTextView(Context context, AttributeSet attr

Android 开发笔记___textvieww__跑马灯效果

1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" android:layout_width="match_parent" 4 android:l

无缝隙滚动跑马灯组件

做前端,遇到个有意思的需求. 无间隙.多列.可控.自定义事件的跑马灯效果. 最终找到了jcarousellite组件. http://www.gmarwaha.com/jquery/jcarousellite/index.php 似乎同事推荐的slick也不错,下次再试试: http://kenwheeler.github.io/slick/

Android textView点击滚动(跑马灯)效果

布局文件: 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 and

ViewGroup实现可以上下、左右滚动跑马灯效果

先上效果图: 动画效果有点差..... 代码: package com.example.scrolltextview; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Paint; import androi