当前位置: 移动技术网 > IT编程>移动开发>Android > Android 使用TextView实现跑马灯效果

Android 使用TextView实现跑马灯效果

2019年01月25日  | 移动技术网IT编程  | 我要评论

dq9金手指,极品娘子榜,祁阳教育网

前言

我们在开发中经常会遇到一个小问题。比如下面一个小例子:

 

这个文字太长,单行中导致无法全部显示出来,这就是今天要实现的功能。 当然,百度中也有很多这种解决方案。

其中有一种,例如:

    <textview
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:singleline="true"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableintouchmode="true"
        android:text="@string/hello_world" />

android:ellipsize="marquee"
android:focusable="true"
android:focusableintouchmode="true"

xml中添加这3行 就能实现效果了。

 

这种方法确实可以实现。

事实上开发过程中,布局是非常复杂和多变的,并不是我们一个textview就能解决所有的布局和要求。

例如,现在用两个textview 

 1 <relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:paddingbottom="@dimen/activity_vertical_margin"
 6     android:paddingleft="@dimen/activity_horizontal_margin"
 7     android:paddingright="@dimen/activity_horizontal_margin"
 8     android:paddingtop="@dimen/activity_vertical_margin"
 9     tools:context=".mainactivity" >
10 
11     <textview
12         android:id="@+id/textview1"
13         android:layout_width="wrap_content"
14         android:layout_height="wrap_content"
15         android:ellipsize="marquee"
16         android:focusable="true"
17         android:focusableintouchmode="true"
18         android:singleline="true"
19         android:text="@string/hello_world" />
20 
21     <textview
22         android:layout_width="wrap_content"
23         android:layout_height="wrap_content"
24         android:layout_below="@id/textview1"
25         android:ellipsize="marquee"
26         android:layout_margintop="20dp"
27         android:focusable="true"
28         android:focusableintouchmode="true"
29         android:singleline="true"
30         android:text="@string/hello_world" />
31 
32 </relativelayout>

这个简单的功能就满足不了了。

第一个跑马灯效果没问题,第二个就没实现了,平常开发中,两个textview都解决不了,平常开发中更解决不了了。

这就是今天我所要讲的内容。

首先,我们先建一个类,继承textview这个类。

 1 package com.example.marqueetextview;
 2 
 3 import android.content.context;
 4 import android.util.attributeset;
 5 import android.view.viewdebug.exportedproperty;
 6 import android.widget.textview;
 7 
 8 public class marqueetext extends textview{
 9 
10     public marqueetext(context context) {
11         super(context);
12     }
13 
14     public marqueetext(context context, attributeset attrs, int defstyle) {
15         super(context, attrs, defstyle);
16     }
17 
18     public marqueetext(context context, attributeset attrs) {
19         super(context, attrs);
20     }
21     @override
22     @exportedproperty(category = "focus")
23     public boolean isfocused() {
24         return true;
25     }
26 }

这个时候实现textview中的一个方法,isfocused(), 返回改成return true。

 然后进xml中 把textview修改成我们自定义的这个控件。

 

 1 <relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:paddingbottom="@dimen/activity_vertical_margin"
 6     android:paddingleft="@dimen/activity_horizontal_margin"
 7     android:paddingright="@dimen/activity_horizontal_margin"
 8     android:paddingtop="@dimen/activity_vertical_margin"
 9     tools:context=".mainactivity" >
10 
11     <com.example.marqueetextview.marqueetext
12         android:id="@+id/textview1"
13         android:layout_width="wrap_content"
14         android:layout_height="wrap_content"
15         android:ellipsize="marquee"
16         android:focusable="true"
17         android:focusableintouchmode="true"
18         android:singleline="true"
19         android:text="@string/hello_world" />
20 
21     <com.example.marqueetextview.marqueetext
22         android:layout_width="wrap_content"
23         android:layout_height="wrap_content"
24         android:layout_below="@id/textview1"
25         android:layout_margintop="20dp"
26         android:ellipsize="marquee"
27         android:focusable="true"
28         android:focusableintouchmode="true"
29         android:singleline="true"
30         android:text="@string/hello_world" />
31 
32 </relativelayout>

然后我们再看一下效果。

已经全部实现成功了。

那这到底是为什么呢? 奥秘就在我们重载的isfocused()这个函数. return true全部强制focused.都有焦点了,就都能实现了

如果没设置,焦点都第一个,第二个就无法实现。

这个是我写的demo:https://pan.baidu.com/s/1m1tghch_r3kfnrem4li1vq

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网