跑马灯效果

当你想让一句很长的话显示一行时,可以用

android:singleLine="true"

但后并没有显示完整,会有省略号,想让他显示完整,可以使用

android:ellipsize="marquee"

会发现省略号没有了,但是仍不能显示完全,因此继续添加

android:focusable="true"
android:focusableInTouchMode="true"

但你发现你复制上面的代码,第二行仍然没有显示完毕,这是因为在默认的情况下,会把焦点放在第一个语句上,因此,第二句话没有显示,想让第二句话也跑马灯一样的显示,就得重建一个类,该类继承了TextView,需要写三个构造方法(可通过快捷键Alt+Insert自动生成)
重要的是必须重写isFocused(),不重写此方法时,仍把焦点放在第一个语句上,第二句话仍然不会完全显示
要想使用自定义控件,需要把TextView换成该类的包名加上该类名

<com.mooc.marqueetextviewdemo.MarqueeText
        android:id="@+id/textView1"
        android:text="我是一个textView,是一个很长的textView,是一个超长的textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"/>

代码解析

android:singleLine="true"//单行显示
android:ellipsize="marquee"//实现跑马灯效果

补充

android:ellipsize = "end"        //省略号在结尾

android:ellipsize = "start"      //省略号在开头

android:ellipsize = "middle"      // 省略号在中间

android:ellipsize = "marquee"     //跑马灯
android:focusable与android:focusableInTouchMode

前者针对在键盘下操作的情况,如果设置为true,则键盘上下左右选中,焦点会随之移动。

而后者,显然是针对触屏情况下的,也就是我们点击屏幕的上的某个控件时,不要立即执行相应的点击逻辑,而是先显示焦点(即控件被选中),再点击才执行逻辑。

android:focusable=“true”不会改变android:focusableInTouchMode,因此只在键盘状态下显示焦点,在TouchMode状态下,依旧无法显示焦点。

android:focusable=“false”,一定会使android:focusableInTouchMode=“false”。

相对的

android:focusableInTouchMode=“false”,不会影响android:focusable。

android:focusableInTouchMode=”true”,一定会是android:focusable=“true”



说来说去有点绕。

但请记住一点,就是对于现在触屏时代的手机而言,如果要获取焦点,我们只需要设置

android:foucusableInTouchMode=“true”就可以了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值