一、在Android studio创建一个空的Android项目
二、编写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">
<!--
match_parent:容器的宽度
wrap_content:根据内容来分配宽度
-->
<TextView
android:id="@+id/tv_one"
android:layout_width="match_parent"
android:layout_height="200dp"
android:text="@string/tv_one"
android:textColor="@color/teal_200"
android:textStyle="bold"
android:textSize="30dp"
android:background="@color/purple_200"
android:gravity="center"
android:shadowColor="@color/purple_500"
android:shadowRadius="0.1"
android:shadowDx="3.0"
android:focusableInTouchMode="true"
android:shadowDy="3.0"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever">
</TextView>
</LinearLayout>
在strings.xml下创建自定义文本
三、实现跑马灯的三种方式
先在TextView里添加三个属性:
android:singleLine="true" //不出现换行
android:ellipsize="marquee" //跑马灯属性
android:marqueeRepeatLimit="marquee_forever" //跑多少次的设定
方式1:
在属性下添加 android:clickable="true"属性;
此方法需要主动去点TextView的内容,不方便!
方式2
创建一个自定义的类使文本聚焦:
package com.example.androin1;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;
import androidx.annotation.Nullable;
public class MyTestView extends TextView {
public MyTestView(Context context) {
super(context);
}
public MyTestView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public MyTestView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public boolean isFocused() {
return true;
}
}
然后修改TextView的名字:
修改名字是你写的类的路径+类名;
然后运行即可!
方式3(最简便的一种):
直接在TextView里加一个即可,不需要别的操作!
注:内容简单,继续努力!