现在的位置: 首页 > 综合 > 正文

XML 实现 TextView的跑马灯效果

2017年01月16日 ⁄ 综合 ⁄ 共 1222字 ⁄ 字号 评论关闭

本文用XML直接实现TextView的跑马灯效果,代码如下:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

   <TextView
        android:id="@+id/marquee_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="大解放了卡机的返利卡几点开房间卡洛斯的积分卡拉接受对方"
        android:textColor="#FF0000"
        
        android:singleLine="true"     
        android:ellipsize="marquee"  
          
        android:marqueeRepeatLimit="marquee_forever"       
        android:focusable="true" 
        android:focusableInTouchMode="true"/>

    <TextView
        android:id="@+id/no_marquee_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"        
        android:text="是快乐积分卡拉接受对方可拉伸积分卡拉就是疯狂地短发的空间按打开附件"
        
        android:ellipsize="marquee"
        android:singleLine="true"
         
        android:marqueeRepeatLimit="marquee_forever"       
        android:focusable="true" 
        android:focusableInTouchMode="true"/>

</LinearLayout>

问题总结:

1.代码中的两个TextView,第一有跑马灯的效果,而第二个没有

原因:只有先设置android:singleLine="true"单行显示,再设置android:ellipsize="marquee"才有跑马灯效果

2.网上看了很多,都说只有设置android:layout_width为固定值,才能显示跑马灯效果,实际不然,正如本例所示,设置wrap_content其实也能实现

抱歉!评论已关闭.