满意答案

00e27ab806e4881f8254fe7ae8741834.png

sseeeido27

2016.02.21

00e27ab806e4881f8254fe7ae8741834.png

采纳率:40%    等级:12

已帮助:6654人

今天要实现的一个效果是在Android中实现一个跑马灯的效果:

在新建一个Android product之后,在布局文件main中替换掉原来的TextView

代码如下:

android:layout_width="60dp"

android:layout_height="wrap_content"

android:textColor="@android:color/black"

android:ellipsize="marquee"

android:focusable="true"

android:focusableInTouchMode="true"

android:marqueeRepeatLimit="marquee_forever"

android:scrollHorizontally="true"

android:text="这是跑马灯的效果这是跑马灯的效果">

其他部分代码不用改动,在模拟器上应该可以看到滚动的显示"这是跑马灯的效果这是跑马灯的效果"。

其中有几个问题是我自己碰到的,列举如下:

1.宽度android:layout_width不可以设置为wrap_content(自适应内容)

2.android:text最好是比较长的字符串,最少要长过自己设置的width大小(我这里是60dp)

3.颜色最好别设置为@android:color/white,否则你什么都看不到,因为背景颜色也是white

4.在xml中,TextView 的属性

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

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

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

android:ellipsize = "marquee" 跑马灯

5.android:scrollHorizontally="true"相信大家英文水平还不错的话都明白这句的意思是水平滚动

6.android:marqueeRepeatLimit="marquee_forever" 看英文同样可以明白,是无限次循环,学习android,英文基础还是必须的。

7.最后还有两句代码没有解释:

android:focusable="true" //让TextView获得焦点

android:focusableInTouchMode="true" //针对触摸屏获得当前焦点

事实上这两句代码我也了解不是特别多,在网上查了下这两个属性,目前理解为:

android:focusable="true"相当于当前Activity打开的时候,让当前控件TextView获得焦点,才可以实现滚动功能

android:focusableInTouchMode="true"和上述属性应该是类似的,但限制应该是指的触摸屏上的意思

对于这两个属性,希望有高人指点下。

10分享举报

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐