Android-TextView跑馬燈效果
阿新 • • 發佈:2018-12-06
1.讓很多文字一行顯示,預設是false,設定為true;
android:singleLine="true"
2.對文字進行裁剪,預設是end裁剪;
android:ellipsize="end"
3.設定文字裁剪為start,設定一行顯示:
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="專一 只專注Android,持之以恆 每天堅持努力學習Android,全身心投入把Android學精,把學習Android演變成興趣愛好"android:singleLine="true" android:ellipsize="start" />
4.如何讓文字滾動起來,按理來說設定 ellipsize="marquee" 就可以滾動起來了,但是系統預設認為無法獲取TextVIew焦點,所以還需要設定能得到焦點
android:singleLine="true"
android:ellipsize="marquee"
TextView tvText = findViewById(R.id.tv_text); tvText.setSelected(true);
5.已經可以滾動起來了,由於系統內部是採用定時器去啟動的,有可能會停止,為了解決不穩定性,還可以設定一個屬性就穩定了
android:marqueeRepeatLimit="marquee_forever"
另外一種方式告訴系統TextView是有焦點的:
<liudeli.ui.all.MyTextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="專一 只專注Android,持之以恆 每天堅持努力學習Android,全身心投入把Android學精,把學習Android演變成興趣愛好" android:singleLine="true" android:ellipsize="marquee" android:marqueeRepeatLimit="marquee_forever" />
public class MyTextView extends TextView { public MyTextView(Context context, AttributeSet attrs) { super(context, attrs); } /** * 重新此方法,是為了告訴系統,TextView可以獲取到焦點啦 * @return */ @Override public boolean isFocused() { return true; } }