TextView的跑马灯显示
2015-09-18 17:06
447 查看
在布局中添加一个TextView:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
当显示的内容非常长的时候,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="@string/hello_world" />
我们发现,增加了singleLine, ellipsize, focuable 和focusableInTouchMode这四个属性以后,TextView就可以实现我们想要的效果了。
singleLine 属性用来指定TextView只能单行显示;
ellipsize 用来指定TextView 的省略方式。这里使用marquee,也就是跑马灯的省略方式;
按道理来说,添加这两个属性应该就够了,为什么还要加上focusable 和focusableInTouchMode 这两个属性呢?我也并不非常明白,我认为是因为marquee的效果只有在当前控件获取焦点以后才有用,所以要添加focusable 和focusableInTouchMode来让TextView获取焦点。
这也能解释另外一个问题:当布局中有多个TextView的时候,只有第一个TextView能显示跑马灯的效果。想要解决这个问题,可以实现一个TextView的子类来继承TextView,重写其中的isFocused方法:
public class MyTextView extends TextView {
public MyTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
// TODO Auto-generated constructor stub
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public MyTextView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@Override
public boolean isFocused() {
return true;
}
}
在这个子类里重写了父类所有的构造方法和isFocused 方法,让其返回true。也就是让两个所有的MyTextView 都获取焦点。
最后将布局中的TextView改成自定义的MyTextView
<com.example.marqueetextviewdemo.MyTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:text="@string/hello_world" />
<com.example.marqueetextviewdemo.MyTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:singleLine="true"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:text="@string/hello_world" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
当显示的内容非常长的时候,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="@string/hello_world" />
我们发现,增加了singleLine, ellipsize, focuable 和focusableInTouchMode这四个属性以后,TextView就可以实现我们想要的效果了。
singleLine 属性用来指定TextView只能单行显示;
ellipsize 用来指定TextView 的省略方式。这里使用marquee,也就是跑马灯的省略方式;
按道理来说,添加这两个属性应该就够了,为什么还要加上focusable 和focusableInTouchMode 这两个属性呢?我也并不非常明白,我认为是因为marquee的效果只有在当前控件获取焦点以后才有用,所以要添加focusable 和focusableInTouchMode来让TextView获取焦点。
这也能解释另外一个问题:当布局中有多个TextView的时候,只有第一个TextView能显示跑马灯的效果。想要解决这个问题,可以实现一个TextView的子类来继承TextView,重写其中的isFocused方法:
public class MyTextView extends TextView {
public MyTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
// TODO Auto-generated constructor stub
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public MyTextView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@Override
public boolean isFocused() {
return true;
}
}
在这个子类里重写了父类所有的构造方法和isFocused 方法,让其返回true。也就是让两个所有的MyTextView 都获取焦点。
最后将布局中的TextView改成自定义的MyTextView
<com.example.marqueetextviewdemo.MyTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:text="@string/hello_world" />
<com.example.marqueetextviewdemo.MyTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:singleLine="true"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:text="@string/hello_world" />
相关文章推荐
- Android实现表情 抓取新浪表情
- 详解Android解析Xml的三种方式——DOM、SAX以及XMLpull
- android开发环境遇到adt无法启动的问题分析及解决方法
- Android开发 旋转屏幕导致Activity重建解决方法
- Android开发技巧之在a标签或TextView控件中单击链接弹出Activity(自定义动作)
- Android开发技巧之ViewStub控件惰性装载
- 分享10个很棒的学习Android开发的网站
- android开发之横向滚动/竖向滚动的ListView(固定列头)
- Android开发技巧之我的菜单我做主(自定义菜单)
- Windows下使用Apache Cordova开发ANDROID之HelloWorld
- 根据根据图片的url怎么取得图片ImageView对象
- [转] ADB server didn't ACK
- android开发的相关链接
- Android数据存储之SharedPreferences 共享首选项/偏好设置
- 基于NDK的Android防破解
- 麦子学院android开发笔记:Android更换RatingBar图片
- android开发入门知识之AIDL详解
- android开发学习如何一体化?从入门到高端开发的路如何走?
- Android入门到精通详解 电子书下载
- --暴走漫画系列--高仿淘宝收货地址(附demo)