Android入门(3)——使用TextView实现跑马灯效果
2015-07-03 12:06
801 查看
1. 在正常情况下使用TextView时,如果文字内容过长,会自动折行:
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" />
2. 那么在设置 android:singleLine="true" 单行显示后,会出现...:
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:singleLine="true" android:text="@string/hello_world" />
3. 用 android:ellipsize="marquee" 属性,虽然没有...了,但是还是不能完全显示文字:
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:singleLine="true" android:ellipsize="marquee" android:text="@string/hello_world" />
4. 再添加focusable和focusableInTouchMode属性就可以实现跑马灯了!:
<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" />
5. 但是当界面比较复杂的时候,情况比较麻烦,例如下面两个TextView控件:
<TextView android:id="@+id/textview1" 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" /> <TextView android:layout_below="@id/textview1" 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实现了跑马灯的效果:
为什么呢为什么呢为什么呢??????
因为只有一个焦点,当焦点在第一个TextView时,第二个就不具有焦点,就不能流动。
6. 解决方法:继承TextView类
第一步:在src中创建一个类,如text类:第二步:让text类继承TextView类,鼠标放在TextView上,先点击import,鼠标再放在text上点add...添加构造函数,一共有三个构造函数的,那么另外两个去哪里找呢,就是在第一个构造函数下面右键-Source-Generate Constructors from Superclass,然后将另外两个构造函数也弄出来。
package com.example.martingtextview; import android.content.Context; import android.util.AttributeSet; import android.widget.TextView; public class text extends TextView{ public text(Context context) { super(context); // TODO Auto-generated constructor stub } public text(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // TODO Auto-generated constructor stub } public text(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } }第三步:然后重写isFocused方法:表示查看是否拥有焦点。我们在这里设置为true,表示都具有焦点。
package com.example.martingtextview; import android.content.Context; import android.util.AttributeSet; import android.view.ViewDebug.ExportedProperty; import android.widget.TextView; public class text extends TextView{ public text(Context context) { super(context); // TODO Auto-generated constructor stub } public text(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // TODO Auto-generated constructor stub } public text(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } @Override public boolean isFocused() { // TODO Auto-generated method stub return true; } }第四步:将TextView类改为自定义类com.example.martingtextview.text,引用我们自己实现的TextView类。
// 这里改了的 <com.example.martingtextview.text android:id="@+id/textview1" 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.martingtextview.text android:layout_below="@id/textview1" 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" />第五步:看效果,同时在动了:
第六步:解释原理:
因为在上面的代码中我们用到focusable属性,如果没有设置isFocused为true,那么第一个TextView拿到焦点以后第二个就不能拿到焦点了,所以第二个就不动。
相关文章推荐
- Android Fragment 真正彻底的解决(下一个)
- Android中保存图片的两种方式
- Android ScrollView遇到的问题
- android TextView设置删除线,超链接,颜色和字体等说介绍,androidtextview
- android实现背景音乐播放
- android如何长时间音乐播放实现MediaPlayer
- Android4.4KK下遮盖p-sensor拨打电话无法自动灭屏的问题分析
- Android4.3 蓝牙BLE初步
- Android四大基本组件介绍与生命周期
- Android DiskLruCache完全解析,硬盘缓存的最佳方案
- android隐藏中间四位手机号
- Android 中的一些Manager
- android sdk离线安装
- Android环境搭建
- 【转】Android 最火的快速开发框架XUtils
- 【转】Android 最火框架XUtils之注解机制详解
- Android开发之Android Annotations框架
- Android数据存储之Preference
- Android解析XML(PULL方式)
- Android layoutWeight解惑