效果
AndroidStudio开发文字跑马灯效果,运行在虚拟机与真机上
实现方法
方法一 (推荐)
继承TextView重写isFoucsed方法,直接返回true。目的是为了默认让TextView获取到焦点。
package com.example.jxd;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;
import androidx.annotation.Nullable;
public class MyTextView extends TextView
{
public MyTextView(Context context) {
super(context);
}
public MyTextView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public MyTextView(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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.example.jxd.MyTextView
android:id="@+id/ABC"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#FFB6C1"
android:text="hello,android studio! 你好,安卓工作室。"
android:textSize="50dp"
android:gravity="center"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"
>
</com.example.jxd.MyTextView>
</LinearLayout>
方法二
默认给TextView焦点
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/ABC"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#FFB6C1"
android:text="hello,android studio! 你好,安卓工作室。"
android:textSize="50dp"
android:gravity="center"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"
>
</TextView>
</LinearLayout>
关注
笔者 - jxd
微信公众号搜索 “码农总动员” 或 微信扫描下方二维码,了解更多你不知道的XX,O(∩_∩)O