Android TextView实现跑马灯效果的方法
作者:showCar 时间:2023-07-30 20:44:12
本文为大家分享一个非常简单但又很常用的控件,跑马灯状态的TextView。当要显示的文本长度太长,又不想换行时用它来显示文本,一来可以完全的显示出文本,二来效果也挺酷,实现起来超级简单,所以,何乐不为。先看下效果图:
代码实现
TextView自带了跑马灯功能,只要把它的ellipsize属性设置为marquee就可以了。但有个前提,就是TextView要处于被选中状态才能有效果,看到这,我们就很自然的自定义一个控件,写出以下代码:
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一样。接下来就可以直接使用了,看下布局:
<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软件编程有所帮助。
标签:android,跑马灯
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java中常用时间的一些相关方法
2022-02-06 11:33:38
Android LayerDrawable超详细讲解
2023-12-03 16:36:30
![](https://img.aspxhome.com/file/2023/7/139067_0s.png)
RocketMQ生产者如何规避故障Broker方式详解
2022-06-23 04:36:10
java selenium使用浏览器调试工具实现方法
2023-07-27 04:04:22
![](https://img.aspxhome.com/file/2023/7/57897_0s.gif)
SpringCloud:feign对象传参和普通传参及遇到的坑解决
2023-02-17 11:18:58
Android中EditText和AutoCompleteTextView设置文字选中颜色方法
2022-12-05 04:45:30
![](https://img.aspxhome.com/file/2023/4/125424_0s.png)
Java抽象类与接口区别详解
2021-06-19 19:22:37
![](https://img.aspxhome.com/file/2023/5/76265_0s.png)
Flutter学习之实现自定义themes详解
2022-04-17 17:20:49
C#编程实现自定义热键的方法
2023-12-05 23:57:18
基于C#实现手机号码归属地接口调用
2022-07-14 09:46:54
java8 集合 多字段 分组 统计个数代码
2022-12-07 21:03:34
![](https://img.aspxhome.com/file/2023/5/72325_0s.jpg)
C#实现Array,List,Dictionary相互转换
2022-10-09 13:51:39
![](https://img.aspxhome.com/file/2023/3/69223_0s.png)
Java反射 JavaBean对象自动生成插入,更新,删除,查询sql语句操作
2022-04-05 20:07:11
Android自定义组件跟随自己手指主动画圆
2022-03-11 10:01:48
![](https://img.aspxhome.com/file/2023/5/122065_0s.jpg)
Android扩大View点击范围的方法
2022-07-21 17:49:29
![](https://img.aspxhome.com/file/2023/1/137811_0s.jpg)
Java面试必备八股文整理
2023-11-29 12:03:50
![](https://img.aspxhome.com/file/2023/8/59068_0s.jpg)
浅谈redis key值内存消耗以及性能影响
2022-11-09 20:35:17
MyBatis找不到mapper文件的实现
2023-12-15 09:22:11
![](https://img.aspxhome.com/file/2023/1/61131_0s.png)
Java调用第三方接口示范的实现
2023-06-27 20:32:10
![](https://img.aspxhome.com/file/2023/8/67468_0s.png)
java开发之闹钟的实现代码
2021-07-08 12:46:49
![](https://img.aspxhome.com/file/2023/5/74905_0s.png)