TextView实现跑马灯的效果
1.解决问题
解决TextView文本字段过长的时候无法显示完全。
2.基本代码
在res--values--strings.xml下添加如下代码,实现显示的文字:
<string name="hello_world">人生若只如初见,何事秋风悲画扇。 等闲变却故人心,却道故人心易变。 骊山语罢清宵半,泪雨零铃终不怨。 何如薄幸锦衣郎,比翼连枝当日愿。 </string>
android:singleLine="true" //使得文本必须在一列排版
3.解决方案
方案一:
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" android:ellipsize="marquee" //该代码实现文字横向滚动 android:focusable="true" //该代码获得焦点,但是是用在非触摸屏上 android:focusableInTouchMode="true" //为了弥补上面的触摸屏问题,增加的代码使得可以在手机等触摸设备显示 android:singleLine="true" />这样就可以实现文本跑马灯的效果了。
方案二:
在src下新建一个类,并且添加所有的构造函数和一个isfocoused方法:
package com.example.textview; import android.content.Context; import android.util.AttributeSet; import android.widget.TextView; public class maketext extends TextView { public maketext(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // TODO Auto-generated constructor stub } public maketext(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } public maketext(Context context) { super(context); // TODO Auto-generated constructor stub } public boolean isFocused(){ return true; //使得每一个被定义的都有一个焦点 } }
<com.example.textview.maketext //使用包名调用 android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" android:singleLine="true" />
本博客基于网络课程完成,旨在学习,有错误请指正!