服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android TextView实现跑马灯效果的方法

Android TextView实现跑马灯效果的方法

2021-05-20 16:40showCar Android

这篇文章主要介绍了Android TextView跑马灯效果实现方法,涉及Android布局文件中相关属性的设置技巧,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文为大家分享一个非常简单但又很常用的控件,跑马灯状态的textview。当要显示的文本长度太长,又不想换行时用它来显示文本,一来可以完全的显示出文本,二来效果也挺酷,实现起来超级简单,所以,何乐不为。先看下效果图:

Android TextView实现跑马灯效果的方法

Android TextView实现跑马灯效果的方法

代码实现

textview自带了跑马灯功能,只要把它的ellipsize属性设置为marquee就可以了。但有个前提,就是textview要处于被选中状态才能有效果,看到这,我们就很自然的自定义一个控件,写出以下代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class marqueetextview extends textview {
 
 public marqueetextview(context con) {
  super(con);
 }
 
 public marqueetextview(context context, attributeset attrs) {
  super(context, attrs);
 }
 
 public marqueetextview(context context, attributeset attrs, int defstyle) {
  super(context, attrs, defstyle);
 }
 
 @override
 public boolean isfocused() {
  // todo auto-generated method stub
  if(geteditabletext().equals(truncateat.marquee)){
   return true;
  }
  return super.isfocused();
 }
}

重写了isfocused方法,并进行判断,只有设置了marqueen属性的才保持选中状态,否则它就跟普通textview一样。接下来就可以直接使用了,看下布局:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<linearlayout 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:orientation="vertical">
 <framelayout
  android:id="@+id/titlebar_layout"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:background="#39ac69" >
  <linearlayout
 
   android:layout_width="match_parent"
   android:layout_height="50dp"
   android:background="#ffffff"
   android:gravity="center_vertical"
   android:orientation="horizontal" >
 
   <imageview
    android:id="@+id/home_location_iv"
    android:layout_width="25dp"
    android:layout_height="27dp"
    android:layout_marginleft="10dp"
    android:scaletype="fitxy"
    android:src="@drawable/icon_place" />
 
   <com.lxj.marqueetextview.marqueetextview
    android:id="@+id/home_location_tv"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginleft="10dp"
    android:layout_marginright="10dp"
    android:layout_weight="1"
    android:ellipsize="marquee"
    android:focusable="true"
    android:focusableintouchmode="true"
    android:gravity="center"
    android:marqueerepeatlimit="marquee_forever"
    android:scrollhorizontally="true"
    android:singleline="true"
    android:text="正在定位..."
    android:textcolor="#39ac69"
    android:textsize="18sp" />
 
   <imageview
    android:id="@+id/home_search_iv"
    android:layout_width="25dp"
    android:layout_height="27dp"
    android:layout_marginright="10dp"
    android:scaletype="fitxy"
    android:src="@drawable/icon_place" />
  </linearlayout>
</framelayout>
</linearlayout>

要注意两点ellipsize属性要设置为”marquee”,行数属性即singleline要设置为true。到此textview的跑马灯效果就实现了。

希望本文对大家学习android软件编程有所帮助。

延伸 · 阅读

精彩推荐