方案有两个

  • 1使用Android原生TextView,优点是简单,缺点是一旦失去焦点,就无法滚动了。
  • 2自定义TextView,缺点是稍微麻烦点,优点是可以一直滚动。

方案1 使用Android原生TextView

布局

    id="@+id/tv"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:ellipsize="marquee"        android:focusable="true"        android:focusableInTouchMode="true"        android:singleLine="true"        android:text="这是超长文本这是超长文本这是超长文本这是超长文本这是超长文本这是超长文本这是超长文本这是超长文本" />

android:ellipsize=”marquee”
//滚动属性
android:focusable=”true”
android:focusableInTouchMode=”true”
//使TextView获取焦点,以便滚动
android:singleLine=”true”
//不设置一行显示是无法滚动的

Java代码

TextView tv = null; tv = (TextView) findViewById(R.id.tv);//后来发现上面这一段是废话,可以不写

网上所说的
tv.setMovementMethod(ScrollingMovementMethod.getInstance());
该方法不写仍然可以滚动

该方案一旦其他控件获得焦点,滚动立即停止

方案2 使用自定义TextView

参考:
http://blog.csdn.net/hua631150873/article/details/73469604

Java代码

import android.content.Context;import android.graphics.Rect;import android.util.AttributeSet;import android.widget.TextView;/** *  * @author zhanghuagang *  */public class AutoMarqueeTextView extends TextView {    public AutoMarqueeTextView(Context context) {        super(context);        setFocusable(true);// 在每个构造方法中,将TextView设置为可获取焦点    }    public AutoMarqueeTextView(Context context, AttributeSet attrs, int defStyle) {        super(context, attrs, defStyle);        setFocusable(true);    }    public AutoMarqueeTextView(Context context, AttributeSet attrs) {        super(context, attrs);        setFocusable(true);    }    @Override    public boolean isFocused() {// 这个方法必须返回true,制造假象,当系统调用该方法的时候,会一直以为TextView已经获取了焦点        return true;    }    @Override    protected void onFocusChanged(boolean focused, int direction,            Rect previouslyFocusedRect) {// 这个方法必须删除其方法体内的实现,也就是让他空实现,也就是说,TextView的焦点获取状态永远都不会改变    }}

布局

    <com.example.testan.AutoMarqueeTextView        android:id="@+id/auto"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:singleLine="true"        android:ellipsize="marquee"        android:text="这是超长文本这是超长文本这是超长文本这是超长文本这是超长文本这是超长文本这是超长文本这是超长文本" />

这里就不需要再获取焦点了

以上是滚动一次完成就停止滚动,如果想永久滚动可在xml添加

android:marqueeRepeatLimit="marquee_forever"

代码已经上传
http://download.csdn.net/download/u011109881/9967405

更多相关文章

  1. Android TextView文本的省略与显示
  2. android 兼容所有刘海屏的方案大全
  3. Android EditText 不自动获取焦点(不自动弹出输入法)
  4. Android控件笔记——在界面中显示及输入文本信息
  5. android TextView 文本过长时用滚动条显示
  6. Android新增AppCompatTextView自适应字体大小和文本宽度
  7. Android 如何让EditText不自动获取焦点

随机推荐

  1. 如何隐藏Android4.0及以上版本的ActionBa
  2. android获取设备存储信息
  3. Android菜鸟日记14-对话框
  4. android 画线
  5. 如何隐藏Android4.0及以上版本的ActionBa
  6. Android(安卓)Studio Exception: Could n
  7. sdcard in emulator
  8. Android应用程序键盘(Keyboard)消息处理机
  9. Android监听来电和去电
  10. ImageLoader: java.io.FileNotFoundExcep