实现文字的跑马灯效果,Textview

1.实现文字的跑马灯效果

关于实现文字的跑马灯效果网上有很多的版本,

接下来我就简单的说两个版本,供猿们参考

1)小妞在LinearLayout布局里写下如下代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical">

    <TextView

        android:id="@+id/textview"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:singleLine="true"

        android:ellipsize="marquee"

        android:focusable="true"

        android:focusableInTouchMode="true"

        android:text="我是一个textview,真的不用怀疑,我就是,你得信"/>

</LinearLayout>

这个时候你会发现文字开始滚动,是不是觉得成功啦,哈哈,别急,在代码的基础上接着加一个textview

2)文字的显示如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical">

    <TextView

        android:id="@+id/textview"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:singleLine="true"

        android:ellipsize="marquee"

        android:focusable="true"

        android:focusableInTouchMode="true"

        android:text="我是一个textview,真的不用怀疑,我就是,你得信"/>

     <TextView

        android:id="@+id/textview1"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:singleLine="true"

        android:ellipsize="marquee"

        android:focusable="true"

        android:focusableInTouchMode="true"

        android:text="我是一个textview,真的不用怀疑,我就是,你得信"/>

</LinearLayout>

这个时候你会发现,只有第一行代码开始滚动,第二行的代码怎么都不动,是不是开始着急呢。。。

3)这就是问题的关键了,接下来在这个路径下新建一个class文件

publicclass text extends TextView {

   public text(Context context, AttributeSet attrs, int defStyle) {

      super(context, attrs, defStyle);

      // TODOAuto-generated constructor stub

   }

   public text(Context context, AttributeSet attrs) {

      super(context, attrs);

      // TODOAuto-generated constructor stub

   }

   public text(Context context) {

      super(context);

      // TODOAuto-generated constructor stub

   }

   @Override

   publicboolean isFocused() {//重写这个方法

      returntrue;

   }

}

4)然后在这个的基础上作出如下的更改:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical">

    <com.example.exercise_first_textview.text

        android:id="@+id/textview"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:singleLine="true"

        android:ellipsize="marquee"

        android:focusable="true"

        android:focusableInTouchMode="true"

        android:text="我是一个textview,真的不用怀疑,我就是,你得信"/>

     <com.example.exercise_first_textview.text

        android:id="@+id/textview1"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:singleLine="true"

        android:ellipsize="marquee"

        android:focusable="true"

        android:focusableInTouchMode="true"

        android:text="我是一个textview,真的不用怀疑,我就是,你得信"/>

</LinearLayout>

这个时候再执行一下程序,就会发现问题解决了,两行文字都开始滚动了。


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值