滚动TextView实现方法
方法1:无法自动获取焦点,需要android:clickable="true"
,点击一下,开始滚动
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#98980709"
android:gravity="center"
android:text="TextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextView"
android:textSize="50dp"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"
android:clickable="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
</TextView>
方法2:自定义view,重写isFocused=true
<com.cg.cg1.constrainlayout.MyTV
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#98980709"
android:gravity="center"
android:text="TextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextView"
android:textSize="50dp"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"
</com.cg.cg1.constrainlayout.MyTV>
package com.cg.cg1.constrainlayout;
import android.content.Context;
import android.util.AttributeSet;
import androidx.annotation.Nullable;
public class MyTV extends androidx.appcompat.widget.AppCompatTextView{
public MyTV(Context context) {
super(context);
}
public MyTV(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public MyTV(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public boolean isFocused() {
return true;
}
}
方法3:使用requestFocus
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#98980709"
android:gravity="center"
android:text="TextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextView"
android:textSize="50dp"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"
<requestFocus/>
</TextView>