android 实现TextView实现跑马灯形式的字体

来源:互联网 发布:天地图框架数据标准 编辑:程序博客网 时间:2024/05/09 13:05

        

        在android中,如何让TextView实现跑马灯效果呢?今天我们就来讲讲如何实现吧。

        <TextView
            android:id="@+id/t1"
            android:layout_width="200dp"
            android:layout_height="wrap_content" 
            android:ellipsize="marquee"
            android:marqueeRepeatLimit="marquee_forever"
            android:scrollX="2dp"
            android:singleLine="true"
            android:text="此处为文本显示区域的宽度此值必须比你的文本宽度要小否则是没有效果的"
            android:textColor="#ff00ff00"  >
        </TextView>

        如何只是这样,不会出现跑马灯效果,我们还要处理一下。

        TextView t= (TextView) findViewById(R.id.t1);  

        t.setTextSize(30);

        t.setHorizontallyScrolling(true);

        t.setFocusable(true);

        t.setFocusableInTouchMode(true);

        因为我们的TextView就能聚焦,才能滚动效果。效果如图一,图二所示:


           (图一)      (图二)


         如果我们不在代码中进行聚焦,我们也可以在布局文件中设置:

         <TextView
            android:id="@+id/t1"
            android:layout_width="200dp"
            android:layout_height="wrap_content" 
            android:ellipsize="marquee"
            android:marqueeRepeatLimit="marquee_forever"
            android:scrollX="2dp"
            android:singleLine="true"
            android:text="此处为文本显示区域的宽度此值必须比你的文本宽度要小否则是没有效果的"
            android:textColor="#ff00ff00" 
            android:clickable="true"  
            android:focusable="true"  
            android:focusableInTouchMode="true">


    -_- @a little a day


0 0