zoukankan      html  css  js  c++  java
  • TextView实现跑马灯效果

     网上有很多跑马灯的介绍,有很多跑马灯的代码。或许我的不是最好的,但是应该很容易明白的。

    我们先来介绍一个跑马灯的代码

     1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     android:layout_width="match_parent"
     3     android:layout_height="match_parent"
     4     android:orientation="vertical">
     5 
     6       <TextView
     7         android:id="@+id/wisdom_tv"
     8         android:layout_width="wrap_content"
     9         android:layout_height="wrap_content"
    10         android:text="你必须非常努力,才能看起来毫不费劲!-Moon同学的励志语言"
    11         android:textSize="24sp"
    12         android:singleLine="true"
    13         android:ellipsize="marquee"
    14         android:focusableInTouchMode="true" 
    15         android:focusable="true"/>
    16       
    17         <!--  
    18           android:ellipsize="start" 省略号在开头
    19           android:ellipsize="middle" 省略号在中间
    20           android:ellipsize="end" 省略号在结尾
    21           android:ellipsize="marquee" 跑马灯显示
    22         -->
    23         <!-- 
    24              android:singleLine="true" 内容只能显示在一行
    25              android:focusableInTouchMode="true" 通过touch来获得focus
    26              android:focusable="true" 是否可以获取焦点
    27          -->
    28 </LinearLayout>

    当然如果是一个跑马灯的话,那么这个就完全可以了,但是在以后的开发中,布局会很复杂的,如果出现两个以上的跑马灯的话,那么重复上面的代码,那么是实现不了的,那么两个以上的应该要怎么做呢?

    layout布局的代码如下

     1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     xmlns:tools="http://schemas.android.com/tools"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent"
     5     android:orientation="vertical" >
     6 
     7     <com.shxt.xkl.MaequeeText
     8         android:layout_width="wrap_content"
     9         android:layout_height="wrap_content"
    10         android:ellipsize="marquee"
    11         android:focusable="true"
    12         android:focusableInTouchMode="true"
    13         android:singleLine="true"
    14         android:text="你必须非常努力,才能看起来毫不费劲,你必须非常努力,才能看起来毫不费劲." />
    15 
    16     <com.shxt.xkl.MaequeeText
    17         android:layout_width="wrap_content"
    18         android:layout_height="wrap_content"
    19         android:ellipsize="marquee"
    20         android:focusable="true"
    21         android:focusableInTouchMode="true"
    22         android:singleLine="true"
    23         android:text="你必须非常努力,才能看起来毫不费劲,你必须非常努力,才能看起来毫不费劲." />
    24 
    25 </LinearLayout>

    在新建一个TextView的子类

     1 public class MaequeeText extends TextView {
     2 
     3     public MaequeeText(Context context) {
     4         super(context);
     5     }
     6 
     7 //    重写所有的构造函数    Source==>Generate Constructors from Superclass
     8     public MaequeeText(Context context, AttributeSet attrs, int defStyle) {
     9         super(context, attrs, defStyle);
    10     }
    11 
    12     public MaequeeText(Context context, AttributeSet attrs) {
    13         super(context, attrs);
    14     }
    15 
    16     @Override
    17     public boolean isFocused() {
    18         return true;
    19 //        自定义设置让focusable为true
    20 //        这个方法相当于在layout中
    21 //        android:focusable="true"
    22 //        android:focusableInTouchMode="true"
    23     }
    24 }

    以上代码就能解决了今后多个跑马灯的问题了,希望对大家有帮助!             

                                                                            ~Moon童鞋

  • 相关阅读:
    头文件里面的ifndef /define/endif的作用
    互联网小知识
    IT技术开发人员获得成功的六大步骤
    ab网站压力测试
    微软校招试题
    C程序编译过程浅析
    makefile变量赋值
    printf的题目
    Oracle命名规范
    javascript(js)小数精度丢失的解决方案
  • 原文地址:https://www.cnblogs.com/xkl520xka/p/5093712.html
Copyright © 2011-2022 走看看