测试系统:Android 4.2
以前只需要设置
android:ellipsize="marquee"
android:singleLine="true"
属性再加上获取焦点就能实现效果了,但是在4.2上没成功。
下面3个TextView在4.2上都有这个效果:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:singleLine="true"
android:text="大手大脚就卡死机读卡快乐到家卡斯柯接口zxczxczfds撒大声地说顶顶顶顶" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:singleLine="true"
android:text="ds搜i阿迪快圣诞节快乐打开骚动哦都sd卡水利电力ds搜i阿迪快圣诞节快乐打开骚动哦都sd卡水利电力ds搜i阿迪快圣诞节快乐打开骚动哦都sd卡水利电力ds搜i阿迪快圣诞节快乐打开骚动哦都sd卡水利电力" />
</LinearLayout>
<TextView
android:layout_width="100dip"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:clickable="true"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:singleLine="true"
android:text="走马灯效果的演示" >
</TextView>
</LinearLayout>
但明显第一个TextView没有ellipsize属性。不明白。
如果去掉clickable属性,失去焦点后点击获取不到焦点这个效果就不会再出现,所以android:clickable属性不能少,而那两个focusable*属性页不能少,不然获取不到焦点。android:singleLine保证文本只有1行,否则这个效果不会被触发。
android:scrollHorizontally="true"属性貌似是让单行文本过长时能够被手指拖动,但是4.2中没发现这个属性的作用。