TextView跑马灯的效果和一些TextView的属性(补充:listView中跑马灯效果)

本文介绍如何在Android应用中实现跑马灯效果,并详细解释TextView的关键属性,包括文字显示方式、颜色设置等。此外,还介绍了如何将跑马灯效果应用于listView组件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

学习TextView,然后给大家分享一下跑马灯的效果跟一些TextView的属性。(补充listView中的跑马灯效果)

先上图


布局文件

 
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ellipsize="marquee"//当文字显示不完全,文字以什么方式显示
    android:focusable="true"//获取焦点
    android:focusableInTouchMode="true"//获取触摸焦点
    android:singleLine="true"//以单文本显示
    android:text="@string/hello_world" //文本显示的内容/>

当然平时遇到开发不可能就一个TextView,但是这种方法只有第一个TextView文本才能动起来,这是因为TextView太多而无法获取到焦点--->


所以就需要这种方法才可以--->

/*自定义TextView 重写isFocused()函数,让他return为true也就是说在重复获取焦点了,这样焦点效果自然也就出来了。*/

public class MarqueeText extends TextView {
    public MarqueeText(Context context) {
        super(context);
    }

    public MarqueeText(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MarqueeText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean isFocused() {
        return true;
    }
}
当然布局文件也是要修改的,这里就举例一个
    <com.example.administrator.textview.MarqueeText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:singleLine="true"
        android:text="@string/hello_world" />
现在效果如下

下面是一些TextView的常用属性(对于初学者)--->

android:height 设置文本区域的高度,支持度量单位:px(像素)/dp/sp/in/mm(毫米)

android:maxHeight 设置文本区域的最大高度

android:minHeight 设置文本区域的最小高度

android:width 设置文本区域的宽度,支持度量单位:px(像素)/dp/sp/in/mm(毫米)

android:maxWidth 设置文本区域的最大宽度

android:minWidth 设置文本区域的最小宽度

android:textColor 设置文本颜色

android:textColorHighlight 被选中文字的底色,默认为蓝色

android:textColorHint 设置提示信息文字的颜色,默认为灰色。与hint一起使用。

android:textColorLink 文字链接的颜色.

android:textScaleX 设置文字之间间隔,默认为1.0f。分别设置0.5f/1.0f/1.5f/2.0f效果如下:

android:textSize 设置文字大小,推荐度量单位”sp”,如”15sp

android:lines 设置文本的行数,设置两行就显示两行,即使第二行没有数据。

android:maxLines 设置文本的最大显示行数,常与width或者layout_width结合使用,超出部分自动换行,超出行数将不显示。

android:gravity 设置文本位置,如设置成“center”,文本将居中显示。

android:hint Text为空时显示的文字提示信息,可通过textColorHint设置提示信息的颜色。

当然在实际的开发中不可能只是单个或者多个TextView 而是跟listView联系起来——>

这样用上面的方法就就不行了,需要跟适配器联系起来。

适配器:



也就是将TextView修改为自定义的方法MarqueeText,这一点没啥大的变动。然后就是用代码控制效果。这样就可以实现listView中的跑马灯了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值