要想让TextView的内容内容实现滚动效果:必须满足
1,text控件的内容大于大于屏幕所显示的宽度:并设置为android:singleLine=”true”
2,设置其滚动的标签:android:ellipsize=”marquee”,(其ellipsize还可以设置为1,none 默认 没有省略号 2,start 开始设置省略号 3,end 结束设置省略号 4,middle 中间设置省略号 5,marquee 滚动显示)
3,TextView只有在获取焦点的时候,才会滚动显示
备注:TextView还有二个特殊的控件:
android:focusable=”true”:表示设置焦点
android:focusableInTouchMode=”true”:表示设置触摸焦点
实现滚动效果代码如下:
**第一种方式:**直接在布局文件中实现
这里写代码片<TextView
android:focusableInTouchMode="true"
android:ellipsize="marquee"
android:text="生活是蜿蜒在山中的小径,坎坷不平,沟崖在侧。摔倒了,要哭就哭吧,怕什么,不心装模作样!这是直率,不是软弱,因为哭一场并不影响赶路,反而能增添一份小心"
android:focusable="true"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
**第二种方式:**在代码中实现:写一个类继承于TextView,并实现其isFocused()方法,并设置返回值为true:见如下代码
这里写代码片
<com.m520it.mobilsafe.view.FocuTextView
android:ellipsize="marquee"
android:id="@+id/tv_home_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="生活是蜿蜒在山中的小径,坎坷不平,沟崖在侧。摔倒了,要哭就哭吧,怕什么,不心装模作样!这是直率,不是软弱,因为哭一场并不影响赶路,反而能增添一份小心"/>
这里写代码片
public class FocuTextView extends TextView {
public FocuTextView(Context context) {
super(context);
}
public FocuTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public FocuTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
**@Override
public boolean isFocused() {
return true;
}**
}
“`