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

    一、要点

    设置四个属性

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

    直接在xml中使用

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:ellipsize="marquee"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:text="人生是一场无休、无歇、无情的战斗,凡是要做个够得上称为人的人,都得时时向无形的敌人作战。" />

      注意:singleLine属性 不能换成 maxlLines

    二、复杂布局

    在复杂的布局中可能不会实现跑马灯效果。例如如下布局中,就只有第一个TextView会有跑马灯效果

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tv1"
            android:singleLine="true"
            android:ellipsize="marquee"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:text="人生是一场无休、无歇、无情的战斗,凡是要做个够得上称为人的人,都得时时向无形的敌人作战。" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tv1"
            android:layout_marginTop="10dp"
            android:singleLine="true"
            android:ellipsize="marquee"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:text="人生是一场无休、无歇、无情的战斗,凡是要做个够得上称为人的人,都得时时向无形的敌人作战。" />
    
    
    </RelativeLayout>

    这时候就需要自定义View,实现跑马灯效果

    自定义MarQueeTextView extents TextView  重写isFocused()方法,返回true

    public class MarqueeText extends TextView {
        public MarqueeText(Context context) {
            super(context);
        }
    
        public MarqueeText(Context context, @Nullable AttributeSet attrs) {
            super(context, attrs);
        }
    
        public MarqueeText(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }
    
        @Override
        public boolean isFocused() {
            return true;
        }
    }

     布局中使用

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <com.example.dhj.marqueedemo.View.MarqueeText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tv1"
            android:singleLine="true"
            android:ellipsize="marquee"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:text="人生是一场无休、无歇、无情的战斗,凡是要做个够得上称为人的人,都得时时向无形的敌人作战。" />
        <com.example.dhj.marqueedemo.View.MarqueeText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tv1"
            android:layout_marginTop="10dp"
            android:singleLine="true"
            android:ellipsize="marquee"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:text="人生是一场无休、无歇、无情的战斗,凡是要做个够得上称为人的人,都得时时向无形的敌人作战。" />
    
    
    </RelativeLayout>

    三、注意

    android:singleLine="true"不能改成
    android:maxLines="1"  不然不会滚动
  • 相关阅读:
    【iOS开发每日小笔记(十二)】仿Facebook登录界面 错误提示抖动 利用CAAnimation设置动画效果
    《跑酷好基友》(英文名:BothLive)隐私政策声明
    【iOS开发每日小笔记(十一)】iOS8更新留下的“坑” NSAttributedString设置下划线 NSUnderlineStyleAttributeName 属性必须为NSNumber
    【iOS开发每日小笔记(十)】自制带圆框的头像 利用在CALayer设置“寄宿图”
    【iOS开发每日小笔记(九)】在子线程中使用runloop,正确操作NSTimer计时的注意点 三种可选方法
    2018/3/29
    2018/3/26
    2018/3/25
    BZOJ[3091] 城市旅行
    2018/3/23
  • 原文地址:https://www.cnblogs.com/wangjiaghe/p/7098535.html
Copyright © 2011-2022 走看看