Android TextView实现跑马灯效果

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.prace3.MainActivity">

    <com.example.prace3.MarqueeTextView
        android:id="@+id/text1"
        android:text="@string/hello"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <com.example.prace3.MarqueeTextView
        android:id="@+id/text2"
        android:text="@string/hello"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:layout_below="@+id/text1"
        android:layout_marginTop="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RelativeLayout>
 
 
与 MainActivity.java 同一包下新建 MarqueeTextView.java 
 
 
package com.example.prace3;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;


public class MarqueeTextView extends TextView {
    //Alt + Insert 快捷键生成构造函数
    public MarqueeTextView(Context context) {
        super(context);
    }

    public MarqueeTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MarqueeTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public MarqueeTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    public boolean isFocused(){
        return true;
    }
}


猜你喜欢

转载自blog.csdn.net/csdngaoqingrui/article/details/79214182