您的位置:首页 > 移动开发 > Android开发

TextView跑马灯的效果和一些TextView的属性(补充:listView中跑马灯效果)

2018-03-09 12:15 393 查看
学习TextView,然后给大家分享一下跑马灯的效果跟一些TextView的属性。(补充listView中的跑马灯效果)
先上图




布局文件
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"//当文字显示不完全,文字以什么方式显示
android:focusable="true"//获取焦点
android:focusableInTouchMode="true"//获取触摸焦点
android:singleLine="true"//以单文本显示
android:text="@string/hello_world"//文本显示的内容/>
当然平时遇到开发不可能就一个TextView,但是这种方法只有第一个TextView文本才能动起来,这是因为TextView太多而无法获取到焦点--->



所以就需要这种方法才可以--->/*自定义TextView重写isFocused()函数,让他return为true也就是说在重复获取焦点了,这样焦点效果自然也就出来了。*/

publicclassMarqueeTextextendsTextView{
publicMarqueeText(Contextcontext){
super(context);
}

publicMarqueeText(Contextcontext,AttributeSetattrs){
super(context,attrs);
}

publicMarqueeText(Contextcontext,AttributeSetattrs,intdefStyleAttr){
super(context,attrs,defStyleAttr);
}

@Override
publicbooleanisFocused(){
returntrue;
}
}当然布局文件也是要修改的,这里就举例一个
<com.example.administrator.textview.MarqueeText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:singleLine="true"
android:text="@string/hello_world"/>
现在效果如下





下面是一些TextView的常用属性(对于初学者)--->

android:height设置文本区域的高度,支持度量单位:px(像素)/dp/sp/in/mm(毫米)
android:maxHeight设置文本区域的最大高度
android:minHeight设置文本区域的最小高度
android:width设置文本区域的宽度,支持度量单位:px(像素)/dp/sp/in/mm(毫米)
android:maxWidth设置文本区域的最大宽度
android:minWidth设置文本区域的最小宽度
android:textColor设置文本颜色
android:textColorHighlight被选中文字的底色,默认为蓝色
android:textColorHint设置提示信息文字的颜色,默认为灰色。与hint一起使用。
android:textColorLink文字链接的颜色.
android:textScaleX设置文字之间间隔,默认为1.0f。分别设置0.5f/1.0f/1.5f/2.0f效果如下:
android:textSize设置文字大小,推荐度量单位”sp”,如”15sp”

android:lines设置文本的行数,设置两行就显示两行,即使第二行没有数据。
android:maxLines设置文本的最大显示行数,常与width或者layout_width结合使用,超出部分自动换行,超出行数将不显示。
android:gravity设置文本位置,如设置成“center”,文本将居中显示。
android:hintText为空时显示的文字提示信息,可通过textColorHint设置提示信息的颜色。
当然在实际的开发中不可能只是单个或者多个TextView而是跟listView联系起来——>




这样用上面的方法就就不行了,需要跟适配器联系起来。

适配器:



也就是将TextView修改为自定义的方法MarqueeText,这一点没啥大的变动。然后就是用代码控制效果。这样就可以实现listView中的跑马灯了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息