ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

3.基本控件 跑马灯效果的TextView

2021-09-04 13:33:53  阅读:153  来源: 互联网

标签:控件 Context 跑马灯 PaomadengClass attrs context public TextView


 

 

我们想要让跑马灯动起来一共有三种方法

首先是第一种方法 用TextView控件

<!--字必须得足够长超出屏幕那跑马灯的效果才明显-->
    <!--字太多了就自动换行了咋办?设置为单行-->
    <!--但是设置为单行之后文字变成了省略号咋办,没事,ellipsize="marquee"就是跑马灯-->
    <!--但是我们现在的跑马灯还是不能动,得设置播放次数 marqueeRepeatLimit-->
    <!--但是我们现在的跑马灯还是不能动,还得设置两个焦点为true-->
    <!--但是我们现在的跑马灯可能还是不能动,就需要我们新建一个类去自定义了-->
    <TextView
        android:id="@+id/tv_three"
        android:text="@string/paomadeng"
        android:textColor="@color/myColor"
        android:textSize="30sp"
        android:gravity="center"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

以上可能动不起来 第二种方法 自定义TextView控件

首先新建一个类,继承TextView并且继承父类方法,然后再继承一下isFocused() 返回true

 

package com.example.onetextview1;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;

import androidx.annotation.Nullable;

@SuppressLint("AppCompatCustomView")
public class PaomadengClass extends TextView {
    public PaomadengClass(Context context) {
        super(context);
    }

    public PaomadengClass(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public PaomadengClass(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public PaomadengClass(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    public boolean isFocused() {
        return true;
    }
}

 

写完之后就可以在xml中通过全类名用我们的自定义控件了

<!--    第二种方法 自定义TextView-->
    <com.example.onetextview1.PaomadengClass
        android:id="@+id/tv_four"
        android:text="@string/paomadeng"
        android:textColor="@color/myColor"
        android:textSize="30sp"
        android:gravity="center"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

 

第三种方法

<!--    第三种方法-->
    <TextView
        android:id="@+id/tv_five"
        android:text="@string/paomadeng"
        android:textColor="@color/myColor"
        android:textSize="30sp"
        android:gravity="center"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <requestFocus/>
    </TextView>

 

标签:控件,Context,跑马灯,PaomadengClass,attrs,context,public,TextView
来源: https://www.cnblogs.com/tuyaojiao/p/15226399.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有