Android--TextView实现跑马灯效果
2015-03-24 21:24
826 查看
学习到一个小例子:用TextView实现长text跑马灯效果
1.首先在layout中创建一个TextView控件,写上基本属性。
2.加入属性singleLine="ture"--使整个文字在同一行内显示
3.加入属性focusable="ture"---使此控件可以成为焦点(可以理解为可以被使用)
4.加入属性focusableInTouchMode="true"---见低页
5.加入属性ellipsize="marquee"---实现跑马灯,一般与singleLine一起写入
6.加入的text属性内的内容足够以致超过屏幕
之后便可以在机器上显示出跑马灯效果,但若有多个TextView,则只能显示第一个
原因:因为在写入focusable时还附带一个默认属性focuse="true",但此属性只默认第一个用此属性的控件默认为"true",所以在接下来的控件中focuse的内容都为"false",所以后面的控件无法实现效果
解决方法:
1.在项目包中(src/的项目包中)自定义一个类
2.使此类继承TextView
3.在此类中补充完构造函数---右键->Source->Generate Constructors from Superclass...将自动补完构造函数
4.写入isFocused()方法---其中return true;
5.将TextView控件的名字改为 app包名.类名 来使用自定义类
至此全部控件将可以实现跑马灯效果。
代码:
自定义类:package com.example.textapp;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;
public class Marquee extends TextView{
public Marquee(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public Marquee(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public Marquee(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
@Override
public boolean isFocused() {
// TODO Auto-generated method stub
return true;
}
}
控件:
<com.example.textapp.Marquee
android:id="@+id/textview1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:height="20dp"
android:singleLine="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:text="@string/long_text"
android:textSize="20sp"
/>
<com.example.textapp.Marquee
android:id="@+id/textview2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:height="20dp"
android:singleLine="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:text="@string/long_text"
android:textSize="20sp"
/>
----------------------------------------------------------------------
------------------------------------------------------------------
android:ellipsize = "end" 省略号在结尾
android:ellipsize = "start" 省略号在开头
android:ellipsize = "middle" 省略号在中间
android:ellipsize = "marquee" 跑马灯
-----------------------------------------------------------------
1.首先在layout中创建一个TextView控件,写上基本属性。
2.加入属性singleLine="ture"--使整个文字在同一行内显示
3.加入属性focusable="ture"---使此控件可以成为焦点(可以理解为可以被使用)
4.加入属性focusableInTouchMode="true"---见低页
5.加入属性ellipsize="marquee"---实现跑马灯,一般与singleLine一起写入
6.加入的text属性内的内容足够以致超过屏幕
之后便可以在机器上显示出跑马灯效果,但若有多个TextView,则只能显示第一个
原因:因为在写入focusable时还附带一个默认属性focuse="true",但此属性只默认第一个用此属性的控件默认为"true",所以在接下来的控件中focuse的内容都为"false",所以后面的控件无法实现效果
解决方法:
1.在项目包中(src/的项目包中)自定义一个类
2.使此类继承TextView
3.在此类中补充完构造函数---右键->Source->Generate Constructors from Superclass...将自动补完构造函数
4.写入isFocused()方法---其中return true;
5.将TextView控件的名字改为 app包名.类名 来使用自定义类
至此全部控件将可以实现跑马灯效果。
代码:
自定义类:package com.example.textapp;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;
public class Marquee extends TextView{
public Marquee(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public Marquee(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public Marquee(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
@Override
public boolean isFocused() {
// TODO Auto-generated method stub
return true;
}
}
控件:
<com.example.textapp.Marquee
android:id="@+id/textview1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:height="20dp"
android:singleLine="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:text="@string/long_text"
android:textSize="20sp"
/>
<com.example.textapp.Marquee
android:id="@+id/textview2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:height="20dp"
android:singleLine="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:text="@string/long_text"
android:textSize="20sp"
/>
----------------------------------------------------------------------
要理解focusableInTouchMode这个属性,首先你得知道,Android不是只面向手机的,它还有可能被安装在电视等非触摸输入设备上.即使是在手机上,目前很多手机也都支持键盘输入了. focusable这种属性,更多的是为了解决非触摸输入的,因为你用遥控器或键盘点击控件,就必然要涉及到焦点的问题,只有可以获得焦点的控件才能响应键盘或者遥控器或者轨迹球的确定事件. 然后再反过来看focusableInTouchMode.这个属性的意思一如字面所述,就是在进入触摸输入模式后,该控件是否还有获得焦点的能力. 可以简单的理解为,用户一旦开始通过点击屏幕的方式输入,手机就进入了"touch mode". focusableInTouchMode这种属性,多半是设给EditText这种即使在TouchMode下,依然需要获取焦点的控件.(请自行想下EditText为什么在touch mode下也需要焦点). 比如Button之类的控件,在touch mode下,就已经没有获取焦点的必要了
------------------------------------------------------------------
android:ellipsize = "end" 省略号在结尾
android:ellipsize = "start" 省略号在开头
android:ellipsize = "middle" 省略号在中间
android:ellipsize = "marquee" 跑马灯
-----------------------------------------------------------------
相关文章推荐
- android用TextView实现跑马灯效果
- android TextView实现实现跑马灯效果,并监听滚动完成动作。
- Android TextView属性及跑马灯效果的实现
- Android中TextView实现垂直滚动(轮换效果,非跑马灯)
- Android常用UI编程_TextView实现跑马灯效果
- TextView属性android:ellipsize实现跑马灯效果,TextView内容过长加省略号,点击显示全部内容
- Android之TextView内容实现跑马灯效果
- Android TextView实现跑马灯效果
- android TextView实现跑马灯效果
- Android实现多个跑马灯效果,多个文本框TextView的跑马灯
- android用TextView实现跑马灯效果
- Android实现多个TextView同时显示跑马灯效果
- android TextView的跑马灯效果的实现
- android TextView实现跑马灯效果
- Android开发 TextView实现跑马灯效果
- Android TextView实现跑马灯效果
- Android实现多个跑马灯效果,多个文本框TextView的跑马灯
- android在Gridview实现Textview跑马灯效果
- Android开发:TextView实现跑马灯效果
- android TextView不用获得焦点实现跑马灯效果 两种方法