当TextView中的文字很长,但又想一行显示完的时候,可以用跑马灯的效果
TextView的属性中可以如下设置:
<com.example.test_textview.MyTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="marquee"
android:focusableInTouchMode="true"
android:focusable="true"
android:text="@string/hello_world" />
一个textView创建出来,如果里面的文字很长,默认是这样:
设置singleLine为true后显示为这样:
ellipsize设置为marquee,focusable和focusableInTouchMode设置为true后,可以得到跑马灯效果:
但是如果在下面再添加一个同样的textView,会发现它不能达到效果,是因为只有第一个textView是focus的。
如果再添加一个EditText,会发现两个第一个TextView也不能动了,因为会默认focus在EditText上面。
可以新定义一个类,继承TextView,重写focus函数,将需要跑马灯效果的TextView focus上。
<com.example.test_textview.MyTextView
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.test_textview.MyTextView
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" />