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,这样就简单实现了单个或多个跑马灯效果。

更多相关文章

  1. Android(安卓)ListView几个比较有用的属性
  2. Android(安卓)动画效果设置
  3. android 横向滚动屏幕实现(1)
  4. Android(安卓)开发源码分享
  5. Android(安卓)开源项目分类汇总
  6. android弹出框2(相当于通知)
  7. 简单的 Android(安卓)拍照并显示以及获取路径后上传
  8. Android(安卓)framework修改----关屏动画效果
  9. 浅谈Java中Collections.sort对List排序的两种方法

随机推荐

  1. ViewModel + SavedState
  2. Android(安卓)利用广播接收器启动服务
  3. Android--Notification
  4. Android(安卓)网络状态操作
  5. Android与Javascript交互示例(三)
  6. Holo Dark TabWidget style for Holo Lig
  7. 图形处理
  8. android canvas 编辑bitmap画虚线
  9. Android:Handler,内部类导致的可能内存泄露
  10. RK3399 编译recovery