zoukankan      html  css  js  c++  java
  • Android 实现多行文本跑马灯效果

    Android TextView 实现跑马灯的效果很简单,只要加三个属性就可以了。

    • android:ellipsize="marquee"  
    • android:focusable="true"  
    • android:focusableInTouchMode="true"     

     

    上面的代码简单实用,但仅限于当前页面只有一个跑马灯的TextView 的实现。如果页面有两个或者是更多的跑马灯效果的时候,下面的就不会在动了。

    找了一下原因,是因为要实现跑马灯的效果,TextView 必须要得到焦点。当一个页面有许多跑马灯的时候,默认的第一个TextView已经把焦点占据了。

    因此别的跑马灯也就无法获取焦点了。

    要想一个页面上同时实现多个跑马灯效果怎么办呢?其实也很简单。

    1. 写一个类继承TextView
    2. 重写里面的isFocused ()方法(直接返回true
    3. 调用自己写的TextView控件

    我们把所以的TextView创建时都强制获取焦点。

    这里只粘贴一下,自定义的类和调用的代码(调用的时候从包名开始引用),其余的地方都是一样的。

    <com.example.demo.MyTextView
            android:id="@+id/textView1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:textSize="20sp"
            android:textColor="#0ff0f0"
            android:ellipsize="marquee"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:text="你是什么1???你是什么2???你是什么3???你是什么4???你是什么5???" />
    
        <com.example.demo.MyTextView
            android:id="@+id/textView2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:textSize="20sp"
            android:textColor="#000fff"
            android:ellipsize="marquee"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:text="我是跑马灯1。。。我是跑马灯2。。。我是跑马灯3。。。我是跑马灯4。。。我是跑马灯5。。。" />
     1 package com.example.demo;
     2 
     3 import android.content.Context;
     4 import android.util.AttributeSet;
     5 import android.view.ViewDebug.ExportedProperty;
     6 import android.widget.TextView;
     7 
     8 public class MyTextView extends TextView {
     9 
    10     public MyTextView(Context context, AttributeSet attrs, int defStyle) {
    11         super(context, attrs, defStyle);
    12         // TODO Auto-generated constructor stub
    13     }
    14 
    15     public MyTextView(Context context, AttributeSet attrs) {
    16         super(context, attrs);
    17         // TODO Auto-generated constructor stub
    18     }
    19 
    20     public MyTextView(Context context) {
    21         super(context);
    22         // TODO Auto-generated constructor stub
    23     }
    24 
    25     @Override
    26     @ExportedProperty(category = "focus")
    27     public boolean isFocused() {
    28         // TODO Auto-generated method stub
    29         return true;
    30     }    
    31 }
    View Code
  • 相关阅读:
    Vue源码学习(二)——生命周期
    Android MediaPlayer
    iOS项目开发实战——iOS网络编程获取网页Html源码
    iOS 开发之IPad的设计与实现
    Netty In Action中文版
    Valid Palindrome
    Jetty 类载入问题处理
    JSONArray和JSONObject的简单使用
    ExtAspNet依据Grid导出Excel
    C++中的指针、数组指针与指针数组、函数指针与指针函数
  • 原文地址:https://www.cnblogs.com/Jett/p/4402864.html
Copyright © 2011-2022 走看看