方案有两个

  • 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手动拖动滚动条快速滑动
  2. [android]EditText的一些设置
  3. Android(安卓)EditText 不自动获取焦点(不自动弹出输入法)
  4. android 学习开始
  5. Android(安卓)TextView跑马灯不动 及属性
  6. android EditText inputType 中文解说
  7. Android:EditText 所有属性
  8. Android(安卓)控件使用参数集锦
  9. android xml属性大全

随机推荐

  1. Android(安卓)实现遮罩
  2. android ListView GridView 单选和复选列
  3. Android Studio App设置TextView文字内容
  4. Android中实现全屏显示的方法
  5. Android 获取SHA1和SHA256的值
  6. 安卓布局文件中控件的各种属性的含义
  7. Android(安卓)WebView在4.4版本以上无法
  8. Mac下Android Studio搭建
  9. Android中的几种网络请求方式详解
  10. MPAndroidChart项目实战(六)——自定义1MPA