最近忽然看到了跑马效果,感觉还挺有意思的,但是查了网上好多例子感觉不够智能,感觉这个写的还不错,分享一下下

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity" >    <com.imooc.marqueetextviewdemo2.MarqueeText        android:id="@+id/textview1"        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"/>   <com.imooc.marqueetextviewdemo2.MarqueeText        android:layout_below="@+id/textview1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginTop="10dp"        android:ellipsize="marquee"         android:focusable="true"        android:focusableInTouchMode="true"         android:singleLine="true"        android:text="@string/hello_world" /></RelativeLayout>

这里要说一下,有些代码,只是添加了

 android:ellipsize="marquee" android:focusable="true" android:focusableInTouchMode="true" android:singleLine="true"

这四句代码,就可以就简单的实现了跑马的效果,但是,但是,但是!当你添加了两个TextView之后,你会发现,只有第一个会实现,就是因为这个Focuse的问题,具体是啥我就只能说的模糊,就不说了

package com.imooc.marqueetextviewdemo2;public class MarqueeText extends TextView {    public MarqueeText(Context context, AttributeSet attrs, int defStyle) {        super(context, attrs, defStyle);        // TODO Auto-generated constructor stub    }    public MarqueeText(Context context, AttributeSet attrs) {        super(context, attrs);        // TODO Auto-generated constructor stub    }    public MarqueeText(Context context) {        super(context);        // TODO Auto-generated constructor stub    }    public boolean isFocused()    {         return true;        }}

但是 新建一个类之后再加上这几句代码,那就可以实现两个一起动,好几个一起动,很方便好用。

最后说 一定要在 activity上 控件里面加上包名 不然不好使
当我第一次使用的时候,会一直报错,找了半天发现包名不对,改正之后就好了。
还有一定要是设定 android:singleLine=”true”

更多相关文章

  1. -Android各版本系统源代码下载
  2. 设置TextView文字居中,代码实现android:layout_gravity
  3. Android Studio中同步代码时报:Received status code 400 from se
  4. android java代码覆盖率
  5. Android对话框风格的Activity代码
  6. Android 删除指定文件代码
  7. Android摄像头视频帧数据测试代码
  8. Android 震动 和 停止 代码

随机推荐

  1. Android使用Application总结
  2. 国内目前最全面的介绍――Android中的Bro
  3. android中AlertDialog 中setView用法的一
  4. Android(安卓)子项目 列表
  5. Android之权限管理
  6. Android知识体系总结
  7. ListView
  8. Android(安卓)手势识别向左,右,上,下,滑动的
  9. android 固定横屏幕竖屏
  10. Android(安卓)popupWindow的使用