Android textView 滚动

本文介绍了两种实现Textview滚动效果的方法:一是通过XML属性配置,包括singleLine、ellipsize等;二是自定义Textview并重写isFocused方法以实现自动获取焦点。

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

Textvie滚动效果

1.第一种方式
    <!-- 
        singleLine : 一行显示
        ellipsize  
            none :省略后面文字
            start : 隐藏前面的文字
            middle : 隐藏中间的文字
            end : 隐藏后面的文字
            marquee : 滚动
        focusableInTouchMode : 触摸获取焦点
        TextView天生是没有点击事件和获取焦点的事件
        focusable : 是否获取焦点操作,true:可以  false:不可以
        marqueeRepeatLimit : 设置滚动次数,marquee_forever : -1  一直滚动
    -->
    <TextView 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="aaassss"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:focusableInTouchMode="true"
        android:focusable="true"
        android:marqueeRepeatLimit="marquee_forever"
        />
2.第二种方式,自定义一个textview,让textview自动获取焦点
    a.创建自定义控件,继承textview
        public class HomeTextView extends TextView {
            //在代码中使用的调用
            public HomeTextView(Context context) {
                super(context);
            }
            //在布局文件中使用的时候调用,比两个参数多了样式
            public HomeTextView(Context context, AttributeSet attrs, int defStyle) {
                super(context, attrs, defStyle);
                // TODO Auto-generated constructor stub
            }
            //在布局文件中使用的时候调用
            //布局文件中的控件都是可以用代码来表示
            //AttributeSet : 保存了控件在布局文件中的所有属性
            public HomeTextView(Context context, AttributeSet attrs) {
                super(context, attrs);
                // TODO Auto-generated constructor stub
            }
            //设置自定义的textview自动获取焦点
            //是否获取焦点
            @Override
            public boolean isFocused() {
                //true:获取焦点,false:不获取焦点
                return true;
            }
        }
    b.再布局文件中使用
        <com.itheima.mobliesafe75.ui.HomeTextView 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="1112223333"
            android:singleLine="true"
            android:ellipsize="marquee"
            android:focusableInTouchMode="true"
            android:marqueeRepeatLimit="marquee_forever"
            />
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值