Android(安卓)实现跑马灯效果
16lz
2022-03-09
Android 中有这样的需求:
1,一个 TextView 内容只允许显示一行;
2,内容一行显示不全,需要用跑马灯效果来显示;
效果图:
单个实现跑马灯代码:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.gyq.marqueedemo.marqueedemo.MainActivity"> <TextView android:id="@+id/tv_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:ellipsize="marquee" android:focusable="true" android:singleLine="true" android:marqueeRepeatLimit="marquee_forever" android:focusableInTouchMode="true" android:text="@string/hello_word"/>LinearLayout>
多个跑马灯效果实现方式:
1,自定义一个类,继承 TextView;
package com.gyq.marqueedemo.marqueedemo.widget;import android.annotation.SuppressLint;import android.content.Context;import android.support.annotation.Nullable;import android.util.AttributeSet;import android.widget.TextView;/** * Created by gyq on 2018/1/5 13:42 */@SuppressLint("AppCompatCustomView")public class MarqueeTextView extends TextView { public MarqueeTextView(Context context) { super(context); } public MarqueeTextView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); } public MarqueeTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override public boolean isFocused() { return true; }}
布局文件:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.gyq.marqueedemo.marqueedemo.MainActivity"> <com.gyq.marqueedemo.marqueedemo.widget.MarqueeTextView android:id="@+id/tv_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:ellipsize="marquee" android:focusable="true" android:singleLine="true" android:marqueeRepeatLimit="marquee_forever" android:focusableInTouchMode="true" android:text="@string/hello_word"/> <com.gyq.marqueedemo.marqueedemo.widget.MarqueeTextView android:id="@+id/tv_content" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:ellipsize="marquee" android:focusable="true" android:singleLine="true" android:marqueeRepeatLimit="marquee_forever" android:focusableInTouchMode="true" android:text="@string/hello_word"/>LinearLayout>
OK,这样就简单实现了单个或多个跑马灯效果。
更多相关文章
- Android(安卓)ListView几个比较有用的属性
- Android(安卓)动画效果设置
- android 横向滚动屏幕实现(1)
- Android(安卓)开发源码分享
- Android(安卓)开源项目分类汇总
- android弹出框2(相当于通知)
- 简单的 Android(安卓)拍照并显示以及获取路径后上传
- Android(安卓)framework修改----关屏动画效果
- 浅谈Java中Collections.sort对List排序的两种方法